Ok, probably we all heard that linq queries are deferred, so be careful bla bla.
Why is it deferred ? i think for performance, and resource use. By what makes linq queries less resource dependent ?
Let's have a quick look at the Where extension method.
public static IEnumerable<source>Where<source>(this IEnumerable<source> sourcelist, Func<source>, Boolen>predicate)
{
foreach(source element in sourcelist)
{
if(predicate(element) yield return element;
}
}
We can see that Where is an extension method (the first parameter is marked with "this" and it is a static function) of any IEnumerable object.
What basically it does is to iterate through each element in the collection (IEnumerable collection), calling the function predicate for each element,
and if the element returns true in the predicate function; return the element. But it is smart enough not to return all the elements in once,
in stead it uses yield. yield retuns the current element and waits for the next call to return the next element, this makes it more effecient.
Also where method returns an IEnumerable object which means the result can be appended to Select, Where etc etc.