ObjectResult<T>.Dispose ObjectResult<T>.Dispose ObjectResult<T>.Dispose ObjectResult<T>.Dispose Method

Definition

Führt Aufgaben aus, die mit dem Freigeben oder dem Zurücksetzen von Ressourcen zusammenhängen.Performs tasks associated with freeing, releasing, or resetting resources.

public:
 override void Dispose();
public override void Dispose ();
override this.Dispose : unit -> unit
Public Overrides Sub Dispose ()

Beispiele

Das Beispiel in diesem Thema basiert auf dem AdventureWorks Sales-Modell.The example in this topic is based on the AdventureWorks Sales Model. Im Beispiel wird ein ObjectResult<T> von der Execute-Methode zurückgegeben.The example returns an ObjectResult<T> from the Execute method. Anschließend wird ein Enumerator abgerufen, und die Abfrageergebnisse werden durchlaufen.Then it gets an enumerator and iterates through the query results. Abschließend werden der Enumerator und das ObjectResult<T>-Objekt freigegeben.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;
    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();
        }
    }
}
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
    Try
        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()
    Finally
        ' Dispose the query results and the enumerator. 
        If queryResults IsNot Nothing Then
            queryResults.Dispose()
        End If
        If enumerator IsNot Nothing Then
            DirectCast(enumerator, IDisposable).Dispose()
        End If
    End Try
End Using

Hinweise

Schließt den DbDataReader, der die Ergebnisse der Abfrage enthält.Closes the DbDataReader, which contains the results of the query.

Gilt für:

Siehe auch