What would be the proper way of optimizing the following kind of statements:
IEnumerable<T> sequence = BuildSequence();
// 'BuildSequence' makes some tough actions and uses 'yield return'
// to form the resulting sequence.
Now, if I'm willing to take only some of the first elements, I could use something like:
sequence.Take(5);
And so, if my sequence from BuildSequence actually contains several thousands of elements, I obviously don't want all of them to be constructed, because I would only need 5 of them.
Does LINQ optimize this kind of operations or I would have to invent something myself?