Enumerable.AsEnumerable<TSource>(IEnumerable<TSource>) Method

Definition

傳回 IEnumerable<T> 類型的輸入。Returns the input typed as IEnumerable<T>.

public:
generic <typename TSource>
[System::Runtime::CompilerServices::Extension]
 static System::Collections::Generic::IEnumerable<TSource> ^ AsEnumerable(System::Collections::Generic::IEnumerable<TSource> ^ source);
public static System.Collections.Generic.IEnumerable<TSource> AsEnumerable<TSource> (this System.Collections.Generic.IEnumerable<TSource> source);
static member AsEnumerable : seq<'Source> -> seq<'Source>
<Extension()>
Public Function AsEnumerable(Of TSource) (source As IEnumerable(Of TSource)) As IEnumerable(Of TSource)

Type Parameters

TSource

source 項目的類型。The type of the elements of source.

Parameters

source
IEnumerable<TSource>

型別為 IEnumerable<T> 的序列。The sequence to type as IEnumerable<T>.

Returns

IEnumerable<TSource>

型別為 IEnumerable<T> 的輸入序列。The input sequence typed as IEnumerable<T>.

Examples

下列程式碼範例示範當需要標準查詢運算子時,如何使用 AsEnumerable<TSource>(IEnumerable<TSource>) 來隱藏類型的自訂 Where 方法。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)
    {
        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.
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 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.
    Console.WriteLine(output.ToString())
End Sub

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

Remarks

AsEnumerable<TSource>(IEnumerable<TSource>) 方法不會影響,而是會將 source 的編譯時間類型從實作為 IEnumerable<T> 的類型變更為 IEnumerable<T> 本身。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>) 可用來在序列執行 IEnumerable<T> 但也有一組不同的可用公用查詢方法時,在查詢執行之間做選擇。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. 例如,假設有一個會執行 IEnumerable<T> 的泛型類別 Table,而且有自己的方法,例如 WhereSelectSelectManyWhere 的呼叫會叫用 Where 的公用 Table方法。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. 代表資料庫資料表的 Table 類型可能會有 Where 的方法,該方法會採用述詞引數做為運算式樹狀結構,並將樹狀結構轉換為 SQL 以進行遠端執行。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. 如果不想要遠端執行,例如因為述詞叫用區域方法,則可以使用 AsEnumerable 方法來隱藏自訂方法,並改為提供標準查詢運算子。If remote execution is not desired, for example because the predicate invokes a local method, the AsEnumerable method can be used to hide the custom methods and instead make the standard query operators available.

Applies to