“或"等效于 Linq Where() lambda 表达式 [英] "Or" equivalent in Linq Where() lambda expression
本文介绍了“或"等效于 Linq Where() lambda 表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Linq 中是否有一种方法可以用来构建 SQL 字符串,例如...where (a=1) OR (a=2)"?
Is there a method in Linq where you can use to build SQL strings like "...where (a=1) OR (a=2)"?
推荐答案
您当然可以在 Where 子句(扩展方法)中进行.但是,如果您需要动态构建复杂的查询,则可以使用 PredicateBuilder.
You can certainly do it within a Where clause (extension method). If you need to build a complex query dynamically, though, you can use a PredicateBuilder.
var query = collection.Where( c => c.A == 1 || c.B == 2 );
或者使用 PredicateBuilder
Or using a PredicateBuilder
var predicate = PredicateBuilder.False<Foo>();
predicate = predicate.Or( f => f.A == 1 );
if (allowB)
{
predicate = predicate.Or( f => f.B == 1 );
}
var query = collection.Where( predicate );
这篇关于“或"等效于 Linq Where() lambda 表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文