Hooked on LINQ

Hooked on LINQ - Developers' Wiki
for .NET Language Integrated Query

Companion book for this site
LINQ to Objects Using C# 4.0:
Using and Extending LINQ to Objects and Parallel LINQ (PLINQ)
Quick Search

Advanced Search »
This operator seemed to be added to the January 2007 Orcas preview. I'm not exactly sure what function it performs; It returns the source it is passed?

This was answered in the comments by a reader:

This conversion operator has no effect other than to change the compile-time type of source from a type that implements IEnumerable, to IEnumerable itself.

AsEnumerable can be used to choose between query operator implementations in cases where a collection implements IEnumerable but also has a different set of public query operators.

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

public static IEnumerable<TSource> AsEnumerable<TSource>(
    this IEnumerable<TSource> source) {
      return source;
}

If you would like to comment on this page, click on the Discuss button located on the top-right of each page. Feel free to edit any mistakes or omissions you find. If you have an objection or find in-appropriate content then contact the administrator. This website is not affiliated with Microsoft®, all content and opinions are those of the specific author and some advice, solutions and article may contain unintentional errors - please use care. Other websites by this author: Focused Objective, Geek Speak Decoded.