views:

1246

answers:

6

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?

+2  A: 

It's just a nicest and shortest way to cast to an IEnumerable. If you look at it in Reflector, you can see it does nothing except return the object as an IEnumerable.

From MSDN:

The AsEnumerable(Of TSource)(IEnumerable(Of TSource)) method has no effect other than to change the compile-time type of source from a type that implements IEnumerable(Of T) to IEnumerable(Of T) itself.

Meta-Knight
I'm not sure this is true. I believe it is to do with query implementation.
runrunraygun
@runrunraygun: It's there because it makes it easier to switch from a IQueryable to IEnumerable, for instance, but it generally doesn't do anything special except than cast it to IEnumerable. If you don't believe me, check in Reflector ;-)
Meta-Knight
@runrunraygun: I provided a quote from the link that YOU provided to prove this ;-)
Meta-Knight
back-pedaling... yeah, think i misunderstood something maybe...
runrunraygun
+17  A: 

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.

Jason
This is true - but wouldn't casting to `IEnumerable<T>` provide the same result?
Erik Forbes
I find `Table.AsEnumerable().Where(somePredicate)` to be far more readable than `((IEnumerable<TableObject>)Table).Where(somePredicate)`.
Jason
We know it is more readable, but are they equivalent?
Jader Dias
Yes, they are equivalent.
Jason
I've got it - they're technically equivalent, but AsEnumerable is more capable due to anonymous types: you can't cast to an IEnumerable of anonymous types, but AsEnumerable can.
Erik Forbes
That's a really great point. I do consider that a side benefit though; I think the main point is readability.
Jason
Perhaps, but using AsEnumerable is the *only* way to do something like what I've got in my example. If you couldn't use AsEnumerable, you'd have to create a new type every time you wanted to do this, and not use anonymous types at all.
Erik Forbes
@Erik: You could, of course, create your own extension method to do the cast to `IEnumerable<anon>`, but in that case you'd effectively be re-implementing `AsEnumerable`.
LukeH
A: 

there is an explanation of query implimentation on msdn.

runrunraygun
+2  A: 

As you say, if a type already implements IEnumerable<T> then there's not really any functional difference between casting to the interface or calling the AsEnumerable method.

My guess, and it's only a guess, is that calling AsEnumerable improves readability and retains the fluent signature of other LINQ extension methods:

var query = ((IEnumerable<YourType>)yourCollection).Select(x => x.YourProperty);

// vs

var query = yourCollection.AsEnumerable().Select(x => x.YourProperty);

It also allows types that don't implement IEnumerable<T> - for example, DataTable - to have their own version of the AsEnumerable extension. This allows you to continue using the same pattern in queries against those types - even though it's a different AsEnumerable method that you're calling - without needing to worry about whether or not the type really implements IEnumerable<T>.

LukeH
But those types that don't implement IEnumerable<T> don't have access to the AsEnumerable extension method - so those types aren't included in the set of types that this question is regarding. Good point nonetheless.
Erik Forbes
@Erik: My point is that you can use *the same pattern* when querying a `DataTable`, for example, even though `DataTable` doesn't implement `IEnumerable<T>`: `var query = yourDataTable.AsEnumerable().Select(x => x["YourColumn"])` etc. http://msdn.microsoft.com/en-us/library/system.data.datatableextensions.asenumerable.aspx
LukeH
Yes - that's why I voted you up. =)
Erik Forbes
+2  A: 

I've thought of a reason apart from readability, though related to query implementation: using Linq to Objects on anonymous types returned via another Linq provider. You can't cast to an anonymous type (or a collection of anonymous types), but you can use .AsEnumerable() to perform the cast for you.

Example:

// Get an IQueryable of anonymous types.
var query = from p in db.PeopleTable /* Assume Linq to SQL */
            select new { Name = p.Name, Age = p.Age };

// Execute the query and pull the results into an IEnumerable of anonymous types
var enum = query.AsEnumerable();

// Use Linq to Objects methods to further refine.
var refined = from p in enum
              select new
              {
                  Name = GetPrettyName(p.Name),
                  DOB = CalculateDOB(p.Age, DateTime.Now)
              };

Clearly the reason here is that we want to use something like Linq to SQL to pull down some records into an anonymous type, then perform some custom logic (that wouldn't be possible via Linq to SQL) using Linq to Objects on the client-side.

Casting to IEnumerable<_anon> isn't possible, so .AsEnumerable() is the only way to go.

Thanks everyone who answered to help me piece this together. =)

Erik Forbes
A: 

Anonymous types are a main reason to provide these kinds of extension methods. (you cannot use anonymous types in generics parameters) But a method call can use type inference allowing you to omit specifying the type in the generic parameters.

james