如何在LINQ WHERE语句中使用OR运算符 [英] How to use OR operator in LINQ WHERE statement
问题描述
我只需要用LINQ编写的TSQL语句的等效项.最好在lambda语句中,但任何方法都可以. TSQL语句:
I just need an equivalent of this TSQL statement written in LINQ. Preferably in lambda statement but anything will work. TSQL statement:
select *
from Table1 as t1
where t1.Column1 = a OR t1.Column2 = b
推荐答案
就像其他C#代码一样,将||
用于OR
Just like other C# code use ||
for OR
方法语法:
var query = db.Table1
.Where(r=> r.Column1 == a || r.Column2 == b);
查询语法:
var query = from r in db.Table1
where r.Column1 == a || r.Column2 == b
select r;
查询语法编译为方法语法.
Query Syntax compiles into Method syntax.
请参阅: LINQ(C#)中的查询语法和方法语法
入门语言集成查询(LINQ)中的大多数查询 文档是使用LINQ声明性查询语法编写的. 但是,必须将查询语法转换为对 编译代码时的.NET公共语言运行时(CLR).
Most queries in the introductory Language Integrated Query (LINQ) documentation are written by using the LINQ declarative query syntax. However, the query syntax must be translated into method calls for the .NET common language runtime (CLR) when the code is compiled.
您应该看到:基本LINQ查询操作(C#)
这篇关于如何在LINQ WHERE语句中使用OR运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!