将Linq查询与AND或or相结合
我有一个问题,如下所示:将Linq查询与AND或or相结合,linq,expression-trees,Linq,Expression Trees,我有一个问题,如下所示: var customers = from c in ctx.Customers; 我可以从UI动态传递过滤器列表 情景1。“和” 我需要的是一种结合的方式 因此,最终结果是: var filterList = from c in ctx.Customers where c.ID == 1 && c.Name == 'XYZ' var filterList = from c in ct
var customers = from c in ctx.Customers;
我可以从UI动态传递过滤器列表
情景1。“和”
我需要的是一种结合的方式
因此,最终结果是:
var filterList = from c in ctx.Customers
where c.ID == 1
&& c.Name == 'XYZ'
var filterList = from c in ctx.Customers
where c.Name == 'ABC'
|| c.Name == 'XYZ'
情景2。“或”
同样的问题
var客户=来自ctx中的c客户
我可以从UI动态传递过滤器列表
groupOp = "OR"
filter 1: where c.Name == 'ABC'
filter 2: where c.Name == 'XYZ'
我需要的是一种结合的方式
因此,最终结果是:
var filterList = from c in ctx.Customers
where c.ID == 1
&& c.Name == 'XYZ'
var filterList = from c in ctx.Customers
where c.Name == 'ABC'
|| c.Name == 'XYZ'
如何将IQueryable与AND或or动态组合。目前我没有要求合并AND和or,所以它只是AND或or。
非常感谢您提供的任何帮助。请使用,即
var predicate=PredicateBuilder.False();
foreach(过滤器中的var过滤器)
{
谓词=谓词。或(过滤器);
}
返回ctx.Customers.Where(谓词);
如果您不想在代码中手动生成所有筛选表达式,您可能会从使用字符串库生成表达式中获益。问题是我的类型是动态的,直到运行时才知道。很抱歉没有提供此信息。您是否查看了我的答案(我编辑了它)中指向动态Linq库的第二个链接?如果使用动态Linq,则不需要在编译时知道类型,因为它都基于字符串。