使用LINQ时,&&和之间有什么区别?和多个where子句? [英] When using LINQ, what is the difference between && and multiple where clauses?

查看:66
本文介绍了使用LINQ时,&&和之间有什么区别?和多个where子句?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是LINQ的新手,昨天发现您可以有多个where子句,例如:

I am new to LINQ and discovered yesterday that you can have multiple where clauses such as:

var items = from object in objectList
where object.value1 < 100  
where object.value2 > 10  
select object;

或者您可以写:

var items = from object in objectList
where object.value1 < 100  
   && object.value2 > 10  
select object;

两者之间有什么区别?

推荐答案

第一个将翻译为:

objectList.Where(o => o.value1 < 100).Where(o=> o.value2 > 10)

第二个将翻译为:

objectList.Where(o => o.value1 < 100 && o.value2 > 10)

因此,在第一个序列中,您将有一个第一个过滤序列被再次过滤(第一个序列包含所有值小于100的对象,第二个序列包含第一个序列中所有值> 10的所有对象) ,而在第二个中,您将在相同的labda表达式中进行相同的比较.对于对象,这是有效的,对于其他提供程序,它取决于表达式的翻译方式.

So, in the first one, you will have a first filtered sequence that is filtered again (first sequence contains all the objects with value < 100, the second one containing all the objects with value > 10 from the first sequence), in while the second one you will do the same comparisons in the same labda expression. This is valid fro Linq to objects, for other providers it depends how the expression is translated.

这篇关于使用LINQ时,&amp;&amp;和之间有什么区别?和多个where子句?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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