Enumerable.AsEnumerable<TSource>(IEnumerable<TSource>) 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
傳回 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<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.
備註
除了將的source
編譯時間類型從實IEnumerable<T>作的類型變更為IEnumerable<T>本身以外,方法AsEnumerable<TSource>(IEnumerable<TSource>)沒有作用。
AsEnumerable<TSource>(IEnumerable<TSource>) 當序列實 IEnumerable<T> 作但也有一組不同的公用查詢方法可用時,可用來選擇查詢實作。 例如,假設有一個泛型類別,該類別Table
會IEnumerable<T>實作 並有自己的方法,例如 Where
、 Select
和 SelectMany
,呼叫 Where
會叫用 的Table
公用Where
方法。 Table
表示資料庫數據表的型別可能會有Where
方法,該方法會採用述詞自變數做為表達式樹狀結構,並將樹狀結構轉換成 SQL 以進行遠端執行。 如果不需要遠端執行,例如,因為述詞會叫用本機方法, AsEnumerable 所以方法可以用來隱藏自定義方法,並改為讓標準查詢運算符可供使用。