Enumerable.AsEnumerable<TSource>(IEnumerable<TSource>) 方法

定義

傳回 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)

類型參數

TSource

元素 source的類型。

參數

source
IEnumerable<TSource>

要輸入的 IEnumerable<T>序列為 。

傳回

IEnumerable<TSource>

輸入序列型為 IEnumerable<T>

範例

以下程式碼範例示範如何在需要標準查詢運算子實作時,隱藏 AsEnumerable<TSource>(IEnumerable<TSource>) 型別的自訂 Where 方法。

// 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.

備註

AsEnumerable<TSource>(IEnumerable<TSource>) 方法除了將編譯時的型 source 別從實作 IEnumerable<T> 型態變更為 IEnumerable<T> 自身外,沒有其他效果。

AsEnumerable<TSource>(IEnumerable<TSource>) 當序列實作同時 IEnumerable<T> 擁有不同公開查詢方法時,可用來選擇查詢實作。 例如,給定一個實Table作且擁有自身方法IEnumerable<T>如 、 WhereSelect的泛型類別SelectMany,呼叫 會Where呼叫 的Where公開Table方法 。 代表資料庫資料表的 Table 型別可以有一個 Where 方法,將謂詞參數作為表達式樹,並將該樹轉為 SQL 以便遠端執行。 若不希望遠端執行,例如謂詞呼叫本地方法, AsEnumerable 該方法可用來隱藏自訂方法,並保留標準查詢運算子。

適用於