简体   繁体   English

Linq PredicateBuilder - 多个OR

[英]Linq PredicateBuilder - Multiple ORs

I'm trying to use the PredicateBuilder, as described here - http://www.albahari.com/nutshell/predicatebuilder.aspx 我正在尝试使用PredicateBuilder,如下所述 - http://www.albahari.com/nutshell/predicatebuilder.aspx

The following code 以下代码

var predicate = PredicateBuilder.False<StreetDTO>();

        predicate = predicate.Or(p => p.Locality.Contains(criteria.Locality));
        predicate = predicate.Or(p => p.Name.Contains(criteria.Name));
        predicate = predicate.Or(p => p.Town.Contains(criteria.Town));

        List<StreetDTO> streetData = StreetData.Instance();

        var streetList = from street in streetData.Where(predicate)
                         select street;

as far as I see this should work, according to the example 根据这个例子,据我所知,这应该有效

var newKids  = Product.ContainsInDescription ("BlackBerry", "iPhone");

var classics = Product.ContainsInDescription ("Nokia", "Ericsson")
                      .And (Product.IsSelling());
var query =
  from p in Data.Products.Where (newKids.Or (classics))
  select p;

but all I get is 但我得到的只是

Error 1 The type arguments for method 'System.Linq.Enumerable.Where(System.Collections.Generic.IEnumerable, System.Func)' cannot be inferred from the usage. 错误1无法从用法中推断出方法'System.Linq.Enumerable.Where(System.Collections.Generic.IEnumerable,System.Func)'的类型参数。 Try specifying the type arguments explicitly. 尝试显式指定类型参数。

I'm trying to gain some understanding in LINQ 'on-the-job', so apologies if this is a simple question. 我正试图在LINQ'在职'获得一些理解,所以如果这是一个简单的问题,请道歉。

Ah; 啊; your list is using IEnumerable<T> extension methods (rather than IQueryable<T> ) - try: 你的列表使用IEnumerable<T>扩展方法(而不是IQueryable<T> ) - 尝试:

var streetList = from street in streetData.AsQueryable().Where(predicate)
                 select street;

Try compiling your predicate: 尝试编译谓词:

var streetList = from street in streetData.Where(predicate.Compile())
                 select street;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM