Does AsEnumerable() cache all result (LINQ)

后端 未结 2 1324
遇见更好的自我
遇见更好的自我 2021-01-28 05:53

When we call a query operator on a sequence, a sequence-specific operator gets called.
I mean if i call Where<>() operator on IEnumerable<>

相关标签:
2条回答
  • 2021-01-28 06:24

    AsEnumerable does nothing other than change the compile-time type of the expression.

    It's implemented like this:

    public static IEnumerable<T> AsEnumerable<T>(this IEnumerable<T> 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.

    0 讨论(0)
  • 2021-01-28 06:35

    IQueryable<T> extends IEnumerable<T>, so the AsEnumerable is redundant. However, if you use it, AsEnumerable is not going to greedily load the collection (that would be a very annoying "feature"). Clearly, Reverse will need the whole thing. Mono implements Enumerable.Reverse using a List<T> and a simple iterator-generator that yields in reverse.

    I believe the only real purpose of AsEnumerable is to force the use of IEnumerable explicit interface implementations.

    0 讨论(0)
提交回复
热议问题