One of the extension methods on IEnumerable<T>
is .AsEnumerable()
. This method converts the enumerable object it was called on into an instance of IEnumerable<T>
. However, since an object must implement IEnumerable<T>
in order to apply to this extension method, converting to IEnumerable<T>
is a simple matter of casting to IEnumerable<T>
. My question is why does this method exist at all?
Example:
List<string> strings = new List<string>() { "test", "test2", "test3" }; IEnumerable<string> stringsEnum1 = strings.AsEnumerable(); IEnumerable<string> stringsEnum2 = (IEnumerable<string>)strings;
In the example above, stringsEnum1
and stringsEnum2
are equivalent. What's the point of the extension method?
Edit: As a corollary, why is there an .AsQueryable()
method when casting to IQueryable<T>
is equivalent?
AsEnumerable<TSource>(IEnumerable<TSource>) can be used to choose between query implementations when a sequence implements IEnumerable<T> but also has a different set of public query methods available.
In LINQ, AsEnumerable() method is used to convert/cast specific types of given list items to its IEnumerable equivalent type.
AsEnumerable preserves deferred execution and does not build an often useless intermediate list. On the other hand, when forced execution of a LINQ query is desired, ToList can be a way to do that. AsQueryable can be used to make an enumerable collection accept expressions in LINQ statements.
IEnumerable vs IQueryable The main difference between IEnumerable and IQueryable in C# is that IQueryable queries out-of-memory data stores, while IEnumerable queries in-memory data. Moreover, IQueryable is part of . NET's System. LINQ namespace, while IEnumerable is in System.
Readability is the main issue here. Consider that
Table.AsEnumerable().Where(somePredicate)
is far more readable than
((IEnumerable<TableObject>)Table).Where(somePredicate).
Or imagine wanting to execute part of the query on the SQL Server and the rest in memory:
Table.Where(somePredicate) .Select(someProjection) .AsEnumerable() .SomethingElse()
versus
((IEnumerable<SomeProjectionType>)Table.Where(somePredicate) .Select(someProjection)) .SomethingElse()
Now, as for why such a method is useful at all think of the example of a Table
in a LINQ to SQL DataContext
. As Table
is an IQueryable
it implements IEnumerable
. When you invoke a Where
method on such a Table
and enumerate through the results, code is executed that eventually causes a SQL statement to be executed on a SQL Server. What AsEnumerable
does is says, no, I don't want to use the LINQ to SQL provider to execute the Where
, I want to use the LINQ to Objects implementation of Where
.
Thus enumerating over
Table.Where(somePredicate)
causes a query to be executed on a SQL Server whereas enumerating over
Table.AsEnumerable().Where(somePredicate)
brings the table represented by Table
into memory and executes the Where
functionality in memory (and not on the SQL Server!)
This is the point of AsEnumerable
: to allow you to hide a specific implementation of IEnumerable
methods and instead use the standard implementation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With