Linq to objects Predicate Builder

后端 未结 1 1269
醉酒成梦
醉酒成梦 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<T, bool> True<T>()  { return f => true;  }
      public static Func<T, bool> False<T>() { return f => false; }
    
      public static Func<T, bool> Or<T>(this Func<T, bool> expr1,
                                         Func<T, bool> expr2)
      {
          return t => expr1(t) || expr2(t);
      }
    
      public static Func<T, bool> And<T>(this Func<T, bool> expr1,
                                         Func<T, bool> expr2)
      {
          return t => expr1(t) && expr2(t);
      }
    }
    
    0 讨论(0)
提交回复
热议问题