ObjectResult<T>.GetEnumerator ObjectResult<T>.GetEnumerator ObjectResult<T>.GetEnumerator ObjectResult<T>.GetEnumerator Method


クエリ結果を反復処理する列挙子を返します。Returns an enumerator that iterates through the query results.

 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)


クエリ結果を反復処理する列挙子。An enumerator that iterates through the query results.


このトピックの例は、 AdventureWorks Sales Modelに基づいています。The example in this topic is based on the AdventureWorks Sales Model. この例では、ObjectResult<T> メソッドから Execute を返します。The example returns an ObjectResult<T> from the Execute method. 次に、列挙子を取得し、クエリ結果を反復処理します。Then it gets an enumerator and iterates through the query results. 最後に、列挙子と ObjectResult<T> オブジェクトを解放します。At the end, it releases the enumerator and the ObjectResult<T> object.

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

    System.Collections.IEnumerator enumerator = null;
        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
        // Dispose the query results and the enumerator.
        if (queryResults != null)
        if (enumerator != null)
Using context As New AdventureWorksEntities()
    Dim query As ObjectSet(Of Product) = context.Products
    Dim queryResults As ObjectResult(Of Product) = Nothing

    Dim enumerator As System.Collections.IEnumerator = Nothing
        queryResults = query.Execute(MergeOption.AppendOnly)

        ' Get the enumerator. 
        enumerator = DirectCast(queryResults, System.Collections.IEnumerable).GetEnumerator()

        ' Iterate through the query results. 
        While enumerator.MoveNext()
            Dim product As Product = DirectCast(enumerator.Current, Product)
            Console.WriteLine("{0}", product.Name)
        End While
        ' Dispose the enumerator 
        DirectCast(enumerator, IDisposable).Dispose()
        ' Dispose the query results and the enumerator. 
        If queryResults IsNot Nothing Then
        End If
        If enumerator IsNot Nothing Then
            DirectCast(enumerator, IDisposable).Dispose()
        End If
    End Try
End Using


不要になった列挙子は破棄する必要があります。The enumerator must be disposed when no longer needed. foreachステートメント(For Each Visual Basic) を使用すると、結果の反復処理が完了したときに列挙子が正しく破棄されるようになります。Using the foreach statement (For Each in Visual Basic) ensures that the enumerator is correctly disposed when iteration over the results is complete.