LINQ到对象predicate生成器 [英] Linq to objects Predicate Builder

查看:144
本文介绍了LINQ到对象predicate生成器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是做使用LINQ到对象的条件查询的最佳方式(不LINQ to SQL中)。

目前我使用这里找到predicate建设者<一个href=\"http://www.albahari.com/nutshell/$p$pdicatebuilder.aspx\">http://www.albahari.com/nutshell/$p$pdicatebuilder.aspx
并通过编制predicate到IEnumerable.Where,似乎很好地工作。

什么,我想解决

举例code:

例如:我有这样的

 字符串关键字1 =测试1;
 字串1关键字=Test3的;        IEnumerable的&LT;&的TestObject GT;测试=新名单&LT;&的TestObject GT;()
                                     {
                                         新的TestObject(){名1 =测试1,名称2 =Test1的},
                                         新的TestObject(){名1 =的Test2,名称2 =的Test2},
                                         新的TestObject(){名1 =Test3的,名称2 =Test3的},                                     };        如果(String.IsNullOrEmpty(关键字1)及!&安培; String.IsNullOrEmpty(关键字2))
            测试= tests.Where(E =&GT; e.Name1.Contains(关键字1));
        否则如果(String.IsNullOrEmpty(1关键字)及!&放大器;!String.IsNullOrEmpty(关键字1))
            测试= tests.Where(E =&GT; e.Name2.Contains(关键字2)|| e.Name1.Contains(关键字1));        返回tests.ToList();


解决方案

只要改变 predicateBuilder 使用委托,而不是前pression树木和使用lambda表达式构建的结果:

 公共静态类代表predicateBuilder
{
  公共静态Func键&LT; T,BOOL&GT;真&LT; T&GT;(){返回F =&GT;真正; }
  公共静态Func键&LT; T,BOOL&GT;假&LT; T&GT;(){返回F =&GT;假; }  公共静态Func键&LT; T,BOOL&GT;或LT; T&GT;(本功能使&LT; T,BOOL&GT;表达式1,
                                     FUNC&LT; T,BOOL&GT;表达式2)
  {
      返回T =&GT;表达式1(T)||表达式2(T);
  }  公共静态Func键&LT; T,BOOL&GT;和&lt; T&GT;(本功能使&LT; T,BOOL&GT;表达式1,
                                     FUNC&LT; T,BOOL&GT;表达式2)
  {
      返回T =&GT;表达式1(T)及和放大器;表达式2(T);
  }
}

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/nutshell/predicatebuilder.aspx and passing the compiled predicate to the IEnumerable.Where and it seems to work nicely.

Example code of what I want to solve:

eg I have this

 string keyword1 = "Test1";
 string keyword2 = "Test3";

        IEnumerable<TestObject> tests = new List<TestObject>()
                                     {
                                         new TestObject() {Name1 = "Test1", Name2 = "Test1"},
                                         new TestObject() {Name1 = "Test2", Name2 = "Test2"},
                                         new TestObject() {Name1 = "Test3", Name2 = "Test3"},

                                     };

        if (!String.IsNullOrEmpty(keyword1) && String.IsNullOrEmpty(keyword2))
            tests = tests.Where(e => e.Name1.Contains(keyword1));
        else if (!String.IsNullOrEmpty(keyword2) && !String.IsNullOrEmpty(keyword1))
            tests = tests.Where(e => e.Name2.Contains(keyword2) || e.Name1.Contains(keyword1));

        return tests.ToList();

解决方案

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);
  }
}

这篇关于LINQ到对象predicate生成器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆