是否可以将两个 Lambda 表达式放入一个查询中?
我想知道是否有一种简单的方法可以将两个 Lambda 表达式放置在单个(Linq/Where)查询中?
例如,我当前使用如下所示的方法调用一个方法:
string testing = "blablabla";
if(testing == "" || testing == null)
我尝试了一些组合,例如:
testing.Where(x => x == ("") || x=> x == null);
但上面的方法不起作用。我知道我可以设置一个返回谓词/布尔值的方法,但是目前我对 Lambda 感兴趣,只是想知道如何实现这一点。
我是否需要链接多个Where方法,或者有没有办法实现多个Lambda?
(ps我知道IsNullOrEmpty,这只是我能想到的第一个例子!)
I was wondering if there is an easy way to place two Lambda expressions in a single (Linq/Where) query?
For example, I currently call a method with something like the following:
string testing = "blablabla";
if(testing == "" || testing == null)
I have tried a few combinations such as:
testing.Where(x => x == ("") || x=> x == null);
But the above doesn't work. I know I can set up a method that returns a predicate/bool, but, at the moment, I am interested in Lambdas and was just wondering how to achieve this.
Do I need to chain multiple Where methods, or is there a way to achieve multiple Lambdas?
(p.s. I know about IsNullOrEmpty, this is just the first example I could think of!)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您始终可以将它们组合成一个 lambda。
You can always combine them to a single lambda.
如果您正在寻找以任意方式组合查询条件的通用方法,可以使用表达式树:
http://msdn.microsoft.com/en-us/library/bb882637.aspx
If you're looking for a general way to combine query conditions in arbitrary ways, you can use expression trees:
http://msdn.microsoft.com/en-us/library/bb882637.aspx