When using LINQ to create a collection of IOrderedQueryable are you overriding the GetEnumerator method?

StackOverflow https://stackoverflow.com/questions/18759269

Question

I understand that if you construct a LINQ query and append it with .ToList() you are executing that LINQ query there and then and returning a list object. However, if you don't do this, you instead get an IOrderedQueryable object which as I understand it, hasn't yet been sorted.

If I then place that collection into a For Each statement, is the list ordered at this point? And does this mean that I have in essence overridden the default implementation of GetEnumerator for that collection?

Was it helpful?

Solution

What you are referring to is known as deferred execution. What this basically entails is that as long as you don't call ToList(), ToArray() or any other method that casts your LINQ query directly to a collection, that query is executed at the moment the variable it is assigned to is used.

Up until the point to when your query is used, the IEnumerableis not enumerated. However, I don't think GetEnumerator overridden in the case of deferred execution.

Here's an MSDN blog regarding this specific subject: http://blogs.msdn.com/b/charlie/archive/2007/12/09/deferred-execution.aspx

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top