ObjectResult<T>.Dispose Metoda

Definice

Provádí úlohy spojené s uvolněním, uvolněním nebo resetováním prostředků.

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

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

Zavře objekt DbDataReader, který obsahuje výsledky dotazu.

Platí pro

Viz také