EF4 查询中的多个 OR 语句
我正在尝试创建一个查询,该查询将执行以下操作:
column like 'a' or column like 'B' or column like 'C' //unknown number of OR
我如何在 EF4 中执行此操作,在 nHibernate 中我将有一个析取。
谢谢。
I'm trying to create a query that would do something like :
column like 'a' or column like 'B' or column like 'C' //unknown number of OR
How can I do this in EF4 , in nHibernate I would have a disjunction.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要动态构建表达式树。看一下 PredicateBuilder。
PredicateBuilder 可以帮助您动态构建表达式树。
You would need to build an Expression Tree dynamically. Take a look at the PredicateBuilder.
The PredicateBuilder can help you to dynamically build expression trees.
如果您想要完全匹配(您的示例仅显示完全匹配),您可以这样做:
它仅在 EF 4+ 中有效,并且被翻译为
column IN ('a', 'b', c')
。对于更高级的通用解决方案,您确实需要动态构建表达式树,如 @Wouter de Kort 提到的那样,或者使用 ESQL 将查询编写为字符串(并且您可以根据需要组合任意数量的字符串)。如果您希望使用复杂的搜索模式进行通配符搜索,则必须使用 ESQL 或至少创建在动态创建的表达式树中重用的模型定义函数。If you want full match (your example show only full match) you can do this:
It works only in EF 4+ and it is translated to
column IN ('a', 'b', c')
. For more advanced generic solution you will really need dynamically build expression tree as mentioned by @Wouter de Kort or use ESQL where you write query as a string (and you can combine as many strings as you need). If you expect wildcard searching with complex search patterns you will have to use ESQL or at least create model defined function reused in dynamically created expression tree.有很多方法可以做到这一点。但您应该了解每一种的优点和缺点。了解如何执行 LINQ to SQL、LINQKit 中的 PredicateBuilder 和存储过程来完成此任务!
http://kalcik。 net/2014/01/05/joining-data-in-memory-with-data-in-database-table/
There are many approaches you can do this. But you should know the advantages and disadvantages for each one. See how performs LINQ to SQL, PredicateBuilder from LINQKit and Stored Procedures to accomplish this task!
http://kalcik.net/2014/01/05/joining-data-in-memory-with-data-in-database-table/