AsEnumerable(TSource) Method

Enumerable.AsEnumerable(Of TSource) Method (IEnumerable(Of TSource))


Returns the input typed as IEnumerable(Of T).

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

Public Shared Function AsEnumerable(Of TSource) (
	source As IEnumerable(Of TSource)
) As IEnumerable(Of TSource)


Type: System.Collections.Generic.IEnumerable(Of TSource)

The sequence to type as IEnumerable(Of T).

Return Value

Type: System.Collections.Generic.IEnumerable(Of TSource)

The input sequence typed as IEnumerable(Of T).

Type Parameters


The type of the elements of source.

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

AsEnumerable(Of TSource)(IEnumerable(Of TSource)) can be used to choose between query implementations when a sequence implements IEnumerable(Of T) but also has a different set of public query methods available. For example, given a generic class Table that implements IEnumerable(Of 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 Wheremethod 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(Of 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(Of TSource)(IEnumerable(Of TSource)) 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))
    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.
End Sub

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

Universal Windows Platform
Available since 4.5
.NET Framework
Available since 3.5
Portable Class Library
Supported in: portable .NET platforms
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Windows Phone
Available since 8.1
Return to top
© 2015 Microsoft