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 则该方法可用于隐藏自定义方法,并改为使标准查询运算符可用。

适用于