-1

Written on msdn:

Returns the input typed as IEnumerable<T>.

I do not understand. Help me to understand this method.

Jeppe Stig Nielsen
  • 60,409
  • 11
  • 110
  • 181
Ts-alan
  • 47
  • 2
  • 6
  • 6
    Jon Skeet did an excellent series on Linq to Objects. Here's [Part 36 - AsEnumerable](http://msmvps.com/blogs/jon_skeet/archive/2011/01/14/reimplementing-linq-to-objects-part-36-asenumerable.aspx) – Damien_The_Unbeliever May 19 '14 at 08:52
  • Explains it perfectly: [http://stackoverflow.com/questions/3628425/ienumerable-vs-list-what-to-use-how-do-they-work](http://stackoverflow.com/questions/3628425/ienumerable-vs-list-what-to-use-how-do-they-work) – markpsmith May 19 '14 at 08:52
  • @Jodrell Apologies, I've managed to wipe your close vote by agreeing and then changing my mind; that wasn't my intention. – Rawling May 19 '14 at 09:03
  • @Rawling, it appears I can't reinstate my vote :-S. No matter. – Jodrell May 19 '14 at 09:34
  • Here is a simple (maybe not so typical) example: `List li = XXX; var reversedWithLinq = li.AsEnumerable().Reverse();`. The meaning of `AsEnumerable` is to "hide" other overloads defined by the actual type or interfaces other than `IEnumerable<>` to be sure the overload used is the one from the extension of `IEnumerable<>`. If you remove the `.AsEnumerable()` from my example, it won't compile since the other method returns `void`. – Jeppe Stig Nielsen May 19 '14 at 09:42

1 Answers1

1

There are three implementations of AsEnumerable.

DataTableExtensions.AsEnumerable

Extends a DataTable to give it an IEnumerable interface so you can use Linq against the DataTable.

Enumerable.AsEnumerable<TSource> and ParallelEnumerable.AsEnumerable<TSource>

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

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. For example, given a generic class Table that implements IEnumerable<T> and has its own methods such as Where, Select, and SelectMany, a call to Where would invoke the public Where method of Table. A Table type that represents a database table could have a Where method that takes the predicate argument as an expression tree and converts the tree to SQL for remote execution. If remote execution is not desired, for example because the predicate invokes a local method, the AsEnumerable<TSource> method can be used to hide the custom methods and instead make the standard query operators available.

In other words.

If I have an

IQueryable<X> sequence = ...;

from a Linq Provider, like Entity Framework, and I do,

sequence.Where(x => SomeUnusualPredicate(x));

that query will be composed on and run on the server. This will fail at runtime because Entity Framework doesn't know how to convert SomeUnusualPredicate into SQL.

If I want that to run the statement with Linq to Objects instead, I do,

sequence.AsEnumerable().Where(x => SomeUnusualPredicate(x));

now the server will return all the data and the Enumerable.Where from Linq to Objects will be used instead of the Query Provider's implementation.

It won't matter that Entity Framework doesn't know how to interpret SomeUnusualPredicate, my function will be used directly. (However, this may be an inefficient approach since all rows will be returned from the server.)

Jodrell
  • 34,946
  • 5
  • 87
  • 124