Is linq more efficient than it appears on the surface?

Posted by Justin984 on Programmers See other posts from Programmers or by Justin984
Published on 2013-10-29T15:13:29Z Indexed on 2013/10/29 16:11 UTC
Read the original article Hit count: 325

Filed under:
|

If I write something like this:

var things = mythings
    .Where(x => x.IsSomeValue)
    .Where(y => y.IsSomeOtherValue)

Is this the same as:

var results1 = new List<Thing>();
foreach(var t in mythings)
    if(t.IsSomeValue)
        results1.Add(t);

var results2 = new List<Thing>();
foreach(var t in results1)
    if(t.IsSomeOtherValue)
        results2.Add(t);

Or is there some magic under the covers that works more like this:

var results = new List<Thing>();
foreach(var t in mythings)
    if(t.IsSomeValue && t.IsSomeOtherValue)
        results.Add(t);

Or is it something completely different altogether?

© Programmers or respective owner

Related posts about c#

Related posts about LINQ