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

Definice

Vrátí vstup zadaný jako 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)

Parametry typu

TSource

Typ elementů .source

Parametry

source
IEnumerable<TSource>

Sekvence, která se má zadat jako IEnumerable<T>.

Návraty

IEnumerable<TSource>

Vstupní sekvence s typem IEnumerable<T>.

Příklady

Následující příklad kódu ukazuje, jak použít AsEnumerable<TSource>(IEnumerable<TSource>) ke skrytí vlastní Where metody typu, když je požadovaná implementace standardního operátoru dotazu.

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

Poznámky

Metoda AsEnumerable<TSource>(IEnumerable<TSource>) nemá žádný jiný účinek, než změnit typ source kompilace z typu, který implementuje IEnumerable<T> do IEnumerable<T> sebe.

AsEnumerable<TSource>(IEnumerable<TSource>) Lze použít k výběru mezi implementacemi dotazů, když se sekvence implementuje IEnumerable<T> , ale má také k dispozici jinou sadu metod veřejného dotazu. Například vzhledem k obecné tříděTable, která implementuje IEnumerable<T> a má své vlastní metody, jako Wherejsou , Selecta SelectMany, volání vyvolá veřejnou Where metodu .TableWhere Table Typ, který představuje tabulku databáze, může mít metoduWhere, která převezme argument predikátu jako strom výrazu a převede strom na SQL pro vzdálené spuštění. Pokud vzdálené spuštění není žádoucí, například proto, že predikát vyvolá místní metodu, lze metodu AsEnumerable použít ke skrytí vlastních metod a místo toho zpřístupnění standardních operátorů dotazu.

Platí pro