IQueryable.Where() 适合在哪里表达?
我对 .NET 中的表达式
的经验非常少,这就是为什么我宁愿问你们。 我该怎么办 - 请参阅下面的评论:
using P = Myclass;
..
System.Linq.Expressions.Expression<Func<P, bool>> myExpression = null;
..
myExpression1 = x => foo1 == true && foo2 == false;
myExpression2 = x => ... ;
..
BinaryExpression resultExpression = System.Linq.Expressions.Expression.OrElse(myExpression1, myExpression2);
..
IQueryable<P> l = l.Where(?resultExpression?); // how to transform BinaryExpression to the suitable type?
谢谢
I'm very low experienced with Expressions
in .NET, that's why I rather ask you guys.
How should I - see comment below:
using P = Myclass;
..
System.Linq.Expressions.Expression<Func<P, bool>> myExpression = null;
..
myExpression1 = x => foo1 == true && foo2 == false;
myExpression2 = x => ... ;
..
BinaryExpression resultExpression = System.Linq.Expressions.Expression.OrElse(myExpression1, myExpression2);
..
IQueryable<P> l = l.Where(?resultExpression?); // how to transform BinaryExpression to the suitable type?
Thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您不能以这种方式将 lambda 一起“或”在一起。你确实想将 lambda 体“或”在一起。这是一种方法:
然后:
You can't "OR" lambdas together that way. You really want to "OR" the lambda bodies together. Here's a method to do that:
Then later:
您可能想看一下 Predicatebuilder:
http://www.albahari.com/ nutshell/predicatebuilder.aspx
Predicatebuilder 允许您以非常干净且易于理解的方式运行一些非常强大的表达式(AND/OR/NOT 等)。对于简单的表达式,我当然只是从头开始滚动它们并应用,但对于复杂的东西......
我非常喜欢它:)
SO 本身的一些链接可能会有所帮助:
LINQ to SQL PredicateBuilder
使用 PredicateBuilder、LINQPad 和运算符 ANY 生成 SQL
you might want to take a wee look at the Predicatebuilder:
http://www.albahari.com/nutshell/predicatebuilder.aspx
the Predicatebuilder allows you to run up some very powerful expressions (AND/OR/NOT etc, etc) in a very clean and easy to understand way. For simple expressions, I do of course just roll them from scratch and apply but for the complex stuff...
I'm quite a fan of it :)
a few links on SO itself that may be helpful:
LINQ to SQL PredicateBuilder
Generated SQL with PredicateBuilder, LINQPad and operator ANY
.Where
方法采用 lambda 表达式作为参数,因此您需要将BinaryExpression
构建为完整的LambdaExpression
。.Where
method takes a lambda expression as a parameter, so you need to build yourBinaryExpression
to a completeLambdaExpression
.它是表达式级别上两个
Func
的组合。一种不太花哨的方法应该是:
Its a combination of two
Func<P, bool>
on expression level.A less fancy way to do the same should be: