Monday, February 21, 2011

LINQ : Whats the purpose of AsEnumerable?

AsEnumerable has a funny method signature:


public static IEnumerable AsEnumerable(
 this IEnumerable source
)

But there is a good reason for this function to exist. The reason is that your collection might not be acually an IEnumerable and infact inherit from IEnumerable. In that case any extension methods (e.g. Where ) overridden in the class that inherits from IEnumerable will get called. Sometimes you just want to call the extension method as implemented for IEnumerable (i.e. LINQ to objects). In this case you can use the AsEnumerable extension method and then call the extension method and linq to objects kicks in instead of Linq2SQL / Linq2Entities etc. 




No comments:

Post a Comment