When we call a query operator on a sequence, a sequence-specific operator gets called.
I mean if i call Where<>()
operator on IEnumerable<>
AsEnumerable
does nothing other than change the compile-time type of the expression.
It's implemented like this:
public static IEnumerable AsEnumerable(this IEnumerable source)
{
return source;
}
It doesn't even check for nullity!
The only point is to change the compile-time type, to force the rest of the query to be executed with the Enumerable extension methods instead of Queryable. It's like a cast - except a cast would often be inconvenient in the middle of a query, and for anonymous types you wouldn't be able to cast it anyway.
So, it's really about what the "source" of the query does when it's enumerated. Does that source load everything into memory before returning any results, or does it stream them? IIRC, LINQ to SQL will actually load everything into memory - but I could be wrong. Of course Reverse
is going to buffer anything anyway, but that doesn't mean it'll use double the memory - in most cases that only means it will buffer references to objects; it doesn't create a new copy of each object.