Linq to objects Predicate Builder

后端 未结 1 1277
醉酒成梦
醉酒成梦 2020-12-03 04:11

What is the best way to do a conditional query using linq to objects(not linq to sql).

Currently I am using the Predicate builder found here http://www.albahari.com/

1条回答
  •  一生所求
    2020-12-03 04:28

    Just change PredicateBuilder to use delegates instead of expression trees and use lambdas to build the results:

    public static class DelegatePredicateBuilder
    {
      public static Func True()  { return f => true;  }
      public static Func False() { return f => false; }
    
      public static Func Or(this Func expr1,
                                         Func expr2)
      {
          return t => expr1(t) || expr2(t);
      }
    
      public static Func And(this Func expr1,
                                         Func expr2)
      {
          return t => expr1(t) && expr2(t);
      }
    }
    

    0 讨论(0)
提交回复
热议问题