Linq PredicateBuilder-多个OR [英] Linq PredicateBuilder - Multiple ORs

查看:225
本文介绍了Linq PredicateBuilder-多个OR的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用PredicateBuilder,如此处所述- http://www. albahari.com/nutshell/predicatebuilder.aspx

I'm trying to use the PredicateBuilder, as described here - http://www.albahari.com/nutshell/predicatebuilder.aspx

以下代码

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;

根据示例,

据我所见应该起作用

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;

但是我得到的只是

错误1方法的类型参数 'System.Linq.Enumerable.Where(System.Collections.Generic.IEnumerable, System.Func)'不能为 从用法推断.尝试 指定类型参数

Error 1 The type arguments for method 'System.Linq.Enumerable.Where(System.Collections.Generic.IEnumerable, System.Func)' cannot be inferred from the usage. Try specifying the type arguments explicitly.

我试图在LINQ的在职"知识上有所了解,如果这是一个简单的问题,请您道歉.

I'm trying to gain some understanding in LINQ 'on-the-job', so apologies if this is a simple question.

推荐答案

Ah;您的列表使用的是IEnumerable<T>扩展方法(而不是IQueryable<T>)-尝试:

Ah; your list is using IEnumerable<T> extension methods (rather than IQueryable<T>) - try:

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

这篇关于Linq PredicateBuilder-多个OR的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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