本文介绍了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;

但是我得到的只是

我试图在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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 23:34