Written on msdn:
Returns the input typed as
IEnumerable<T>
.
I do not understand. Help me to understand this method.
Written on msdn:
Returns the input typed as
IEnumerable<T>
.
I do not understand. Help me to understand this method.
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 implementsIEnumerable<T>
toIEnumerable<T>
itself.
AsEnumerable<TSource>(IEnumerable<TSource>)
can be used to choose between query implementations when a sequence implementsIEnumerable<T>
but also has a different set of public query methods available. For example, given a generic classTable
that implementsIEnumerable<T>
and has its own methods such asWhere
,Select
, andSelectMany
, a call toWhere
would invoke the publicWhere
method ofTable
. ATable
type that represents a database table could have aWhere
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, theAsEnumerable<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.)