小编the*_*ode的帖子

C#Linq:将多个.Where()与* OR *子句组合

我一直在搜索有关当前问题的大量信息,但找不到解决该问题的真正答案。

我正在尝试生成一个生成以下SQL的LINQ查询:

SELECT * FROM TABLE WHERE (Field1 = X, Field2 = Y ... ) or (Field3 = Z)
Run Code Online (Sandbox Code Playgroud)

在正常情况下,我只会这样做:

Object.Where(c => (c.Field1 == X && c.Field2 == Y) || (c.Field3 == Z))
Run Code Online (Sandbox Code Playgroud)

我不能使用这种方法,因为查询是通过使用多个.Where()调用来构建的。

有一个例子:

// This is a short example, the real world situation has 20 fields to check and they are all connected with an AND.
if (model.Field1.HasValue) 
{
    Query = Query.Where(c => c.Field1 == X)
}

if (model.Field2.HasValue) 
{
    Query = Query.Where(c => c.Field2 == …
Run Code Online (Sandbox Code Playgroud)

.net c# linq where

13
推荐指数
3
解决办法
2481
查看次数

标签 统计

.net ×1

c# ×1

linq ×1

where ×1