ObjectResult<T>.GetEnumerator Metodo

Definizione

Restituisce un enumeratore con cui è possibile scorrere i risultati della query.

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)

Restituisce

Enumeratore con cui è possibile scorrere i risultati della query.

Implementazioni

Esempio

In questo esempio viene restituito un ObjectResult<T> oggetto dal Execute metodo . Viene quindi ottenuto un enumeratore e vengono scorsi i risultati della query. Infine, vengono rilasciati l'enumeratore e l'oggetto 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();
        }
    }
}

Commenti

L'enumeratore deve essere eliminato quando non è più necessario. L'utilizzo dell'istruzione foreach (For Each in Visual Basic) garantisce che l'enumeratore venga eliminato correttamente al termine dell'iterazione sui risultati.

Si applica a

Vedi anche