Bagikan melalui


ObjectResult<T>.Dispose Metode

Definisi

Melakukan tugas yang terkait dengan membebaskan, melepaskan, atau mereset sumber daya.

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

Contoh

Contoh ini mengembalikan ObjectResult<T> dari Execute metode . Kemudian mendapatkan enumerator dan melakukan iterasi melalui hasil kueri. Pada akhirnya, ia melepaskan enumerator dan ObjectResult<T> objek .

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

Keterangan

DbDataReaderMenutup , yang berisi hasil kueri.

Berlaku untuk

Lihat juga