ObjectResult<T>.GetEnumerator Metoda

Definice

Vrátí enumerátor, který prochází výsledky dotazu.

public:
 virtual System::Collections::Generic::IEnumerator<T> ^ GetEnumerator();
public System.Collections.Generic.IEnumerator<T> GetEnumerator ();
abstract member GetEnumerator : unit -> System.Collections.Generic.IEnumerator<'T>
override this.GetEnumerator : unit -> System.Collections.Generic.IEnumerator<'T>
Public Function GetEnumerator () As IEnumerator(Of T)

Návraty

Enumerátor, který prochází výsledky dotazu.

Implementuje

Příklady

Tento příklad vrátí hodnotu ObjectResult<T> z Execute metody . Pak získá enumerátor a iteruje výsledky dotazu. Na konci uvolní enumerátor a ObjectResult<T> objekt .

using (AdventureWorksEntities context =
    new AdventureWorksEntities())
{
    ObjectSet<Product> query = context.Products;
    ObjectResult<Product> queryResults = null;

    System.Collections.IEnumerator enumerator = null;
    try
    {
        queryResults = query.Execute(MergeOption.AppendOnly);

        // Get the enumerator.
        enumerator = ((System.Collections.IEnumerable)queryResults).GetEnumerator();

        // Iterate through the query results.
        while (enumerator.MoveNext())
        {
            Product product = (Product)enumerator.Current;
            Console.WriteLine("{0}", product.Name);
        }
        // Dispose the enumerator
        ((IDisposable)enumerator).Dispose();
    }
    finally
    {
        // Dispose the query results and the enumerator.
        if (queryResults != null)
        {
            queryResults.Dispose();
        }
        if (enumerator != null)
        {
            ((IDisposable)enumerator).Dispose();
        }
    }
}

Poznámky

Enumerátor musí být odstraněn, pokud již není potřeba. foreach Pomocí příkazu (For Each v jazyce Visual Basic) zajistíte, že se po dokončení iterace výsledků správně odstraní enumerátor.

Platí pro

Viz také