ObjectResult<T>.GetEnumerator Método

Definición

Devuelve un enumerador que recorre en iteración los resultados de la consulta.

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)

Devoluciones

Enumerador que recorre en iteración los resultados de la consulta.

Implementaciones

Ejemplos

En este ejemplo se devuelve un ObjectResult<T> objeto del Execute método . A continuación, obtiene un enumerador y recorre en iteración los resultados de la consulta. Por último, libera el enumerador y el objeto ObjectResult<T>.

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();
        }
    }
}

Comentarios

Se debe eliminar el enumerador cuando ya no se necesite. El uso de la foreach instrucción (For Each en Visual Basic) garantiza que el enumerador se elimine correctamente cuando se complete la iteración sobre los resultados.

Se aplica a

Consulte también