Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
614 views
in Technique[技术] by (71.8m points)

c# - When using LINQ, what is the difference between && and multiple where clauses?

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;

Or you can write:

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

What is the difference between the two?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The first one will be translated into:

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

while the second one will be translated in:

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

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...