__John_ wrote:
It uses a 'lazy' strategy, ie. only evaluating an expression or executing a function when the result is actually needed, am I right?
Yes you are right. It's a principle of Linq to defer execution until is needed. And Linq also evaluate only the elements needed to return the result Let's take Wayne List and do some examples
List peoples = new List {new Person("wayne"), new Person("sarah"), new Person("mark"), new Person("simon"), new Person("ashleigh"), new Person("dave"), new Person("connor"), new Person("bronwyn"), new Person("chantelle"), new Person("will"), new Person("chris")};
int Count = people.Count(p => p.Name.StartsWith("w")); //<-- Count is a greedy operator and all persons are evaluated for a result of 2.
var firstperson = people.Where(p => p.Name.Length == 5).Take(3);
foreach (var p in firstperson) // <-- evaluation start where and only Wayne, Sarah, Mark and Simon will be evaluated. The rest of the list is left alone
{
Console.WriteLine(p.Name);
}
// And to show you when Linq expression are evaluated, try
var firstperson2 = people.Where(p => p.Name.Length == 5).Take(3);
people.Insert(1, new Person("Vince"));
foreach (var p in firstperson) // <-- evaluation start where and only Wayne, Vince ans Sarah, will be evaluated. The rest of the list is left alone
{
Console.WriteLine(p.Name);
}
http://blogs.msdn.com/b/charlie/archive/2007/12/09/deferred-execution.aspx[^]
__John_ wrote:
BTW: How can I enumerate the results more that once?
If you use a Enumerator, you can use Reset to set the enumerator to its initial position, which is before the first element in the collection. But if you use a foreach loop, you can reuse an Enumerable many times. The foreach loop will start at the beginning every time.
Vince Remember the dead, fight for the living