Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

Enumerable.AsEnumerable<TSource> Method

Returns the input typed as IEnumerable<T>.

Namespace:  System.Linq
Assembly:  System.Core (in System.Core.dll)

public static IEnumerable<TSource> AsEnumerable<TSource>(
	this IEnumerable<TSource> source
)

Type Parameters

TSource

The type of the elements of source.

Parameters

source
Type: System.Collections.Generic.IEnumerable<TSource>
The sequence to type as IEnumerable<T>.

Return Value

Type: System.Collections.Generic.IEnumerable<TSource>
The input sequence typed as IEnumerable<T>.

Usage Note

In Visual Basic and C#, you can call this method as an instance method on any object of type IEnumerable<TSource>. When you use instance method syntax to call this method, omit the first parameter.

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.

The following code example demonstrates how to use AsEnumerable<TSource>(IEnumerable<TSource>) to hide a type's custom Where method when the standard query operator implementation is desired.


   // Custom class.
   class Clump<T> : List<T>
   {
      // Custom implementation of Where().
      public IEnumerable<T> Where(Func<T, bool> predicate)
      {
         outputBlock.Text += "In Clump's implementation of Where()." + "\n";
         return Enumerable.Where(this, predicate);
      }
   }

   static void AsEnumerableEx1()
   {
      // Create a new Clump<T> object.
      Clump<string> fruitClump =
          new Clump<string> { "apple", "passionfruit", "banana", 
                 "mango", "orange", "blueberry", "grape", "strawberry" };

      // First call to Where():
      // Call Clump's Where() method with a predicate.
      IEnumerable<string> query1 =
          fruitClump.Where(fruit => fruit.Contains("o"));

      outputBlock.Text += "query1 has been created.\n" + "\n";

      // Second call to Where():
      // First call AsEnumerable() to hide Clump's Where() method and thereby
      // force System.Linq.Enumerable's Where() method to be called.
      IEnumerable<string> query2 =
          fruitClump.AsEnumerable().Where(fruit => fruit.Contains("o"));

      // Display the output.
      outputBlock.Text += "query2 has been created." + "\n";
   }

   // This code produces the following output:
   //
   // In Clump's implementation of Where().
   // query1 has been created.
   //
   // query2 has been created.



Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

Community Additions

Show:
© 2014 Microsoft