Skip to main content
.NET Framework Class Library
EnumerableAsEnumerableTSource Method

Returns the input typed as IEnumerableT.

Namespace:   System.Linq
Assemblies:   System.Linq (in System.Linq.dll)
  System.Core (in System.Core.dll)
Syntax
<[%$TOPIC/bb335435_en-us_VS_110_2_0_0_0_0%]> _
Public Shared Function AsEnumerable(Of TSource) ( _
	source As [%$TOPIC/bb335435_en-us_VS_110_2_0_0_0_1%](Of TSource) _
) As [%$TOPIC/bb335435_en-us_VS_110_2_0_0_0_2%](Of TSource)
public static [%$TOPIC/bb335435_en-us_VS_110_2_0_1_0_0%]<TSource> AsEnumerable<TSource>(
	this [%$TOPIC/bb335435_en-us_VS_110_2_0_1_0_1%]<TSource> source
)
[[%$TOPIC/bb335435_en-us_VS_110_2_0_2_0_0%]]
public:
generic<typename TSource>
static [%$TOPIC/bb335435_en-us_VS_110_2_0_2_0_1%]<TSource>^ AsEnumerable(
	[%$TOPIC/bb335435_en-us_VS_110_2_0_2_0_2%]<TSource>^ source
)
static member AsEnumerable : 
        source:[%$TOPIC/bb335435_en-us_VS_110_2_0_3_0_0%]<'TSource> -> [%$TOPIC/bb335435_en-us_VS_110_2_0_3_0_1%]<'TSource> 
JScript does not support generic types or methods.

Type Parameters

TSource

The type of the elements of source.

Parameters

source
Type: System.Collections.GenericIEnumerableTSource

The sequence to type as IEnumerableT.

Return Value

Type: System.Collections.GenericIEnumerableTSource
The input sequence typed as IEnumerableT.

Usage Note

In Visual Basic and C#, you can call this method as an instance method on any object of type IEnumerableTSource. When you use instance method syntax to call this method, omit the first parameter. For more information, see Extension Methods (Visual Basic) or Extension Methods (C# Programming Guide).
Remarks

The AsEnumerableTSource(IEnumerableTSource) method has no effect other than to change the compile-time type of source from a type that implements IEnumerableT to IEnumerableT itself.

AsEnumerableTSource(IEnumerableTSource) can be used to choose between query implementations when a sequence implements IEnumerableT but also has a different set of public query methods available. For example, given a generic class Table that implements IEnumerableT 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 AsEnumerableTSource method can be used to hide the custom methods and instead make the standard query operators available.

Examples

The following code example demonstrates how to use AsEnumerableTSource(IEnumerableTSource) to hide a type's custom Where method when the standard query operator implementation is desired.

Dim output As New System.Text.StringBuilder

    ' A custom class. 
    Class Clump(Of T)
        Inherits List(Of T)

        ' Constructor. 
        Public Sub New(ByVal collection As System.Collections.Generic.IEnumerable(Of T))
            MyBase.New(collection)
        End Sub 

        ' Custom implementation of Where(). 
        Function Where(ByVal predicate As Func(Of T, Boolean)) As IEnumerable(Of T)
            output.AppendLine("In Clump's implementation of Where().")
            Return Enumerable.Where(Me, predicate)
        End Function 
    End Class 

    Sub AsEnumerableEx1()
        ' Create a new Clump(Of T) object. 
        Dim fruitClump As New Clump(Of String)(New String() _
                                               {"apple", "passionfruit", "banana", _
                                                "mango", "orange", "blueberry", _
                                                "grape", "strawberry"})

        ' First call to Where(): 
        ' Call Clump's Where() method with a predicate. 
        Dim query1 As IEnumerable(Of String) = _
            fruitClump.Where(Function(fruit) fruit.Contains("o"))
        output.AppendLine("query1 has been created." & vbCrLf)

        ' 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. 
        Dim query2 As IEnumerable(Of String) = _
            fruitClump.AsEnumerable().Where(Function(fruit) fruit.Contains("o"))
        output.AppendLine("query2 has been created.")

        ' Display the output.
        MsgBox(output.ToString())
    End Sub 

    ' This code produces the following output: 
    ' 
    ' In Clump's implementation of Where(). 
    ' query1 has been created. 
    ' 
    ' query2 has been created.
// Custom class. 
        class Clump<T> : List<T>
        {
            // Custom implementation of Where(). 
            public IEnumerable<T> Where(Func<T, bool> predicate)
            {
                Console.WriteLine("In Clump's implementation of Where().");
                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"));

            Console.WriteLine("query1 has been created.\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.
            Console.WriteLine("query2 has been created.");
        }

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

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

XNA Framework

Supported in: 3.0

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone Silverlight 8.1, Windows Phone Silverlight 8

Portable Class Library

Supported in: Portable Class Library