Creating a common predicate function

前端 未结 5 1302
南方客
南方客 2020-12-14 01:17

Firstly, I am not sure what terms to use to ask this question, which is probably why I have not found an answer from searching myself.

So I am working with Linq to S

5条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-14 01:34

    Have you tried PredicateBuilder? I haven't used it in over a year, but I found it effective when writing "Or Where" Queries.

    http://www.albahari.com/nutshell/predicatebuilder.aspx

    An example from their page:

    IQueryable SearchProducts (params string[] keywords)
    {
      var predicate = PredicateBuilder.False();
    
      foreach (string keyword in keywords)
      {
        string temp = keyword;
        predicate = predicate.Or (p => p.Description.Contains (temp));
      }
      return dataContext.Products.Where (predicate);
    }
    

提交回复
热议问题