LINQ的:"或"凡当量() [英] Linq: "Or" equivalent of Where()

查看:134
本文介绍了LINQ的:"或"凡当量()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有在LINQ的一个方法,你可以用它来构建SQL字符串,如...其中(A = 1)或(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的:&QUOT;或&QUOT;凡当量()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆