ObjectQuery<T>.Execute(MergeOption) 方法

定義

使用指定的合併選項執行物件查詢。Executes the object query with the specified merge option.

public:
 System::Data::Objects::ObjectResult<T> ^ Execute(System::Data::Objects::MergeOption mergeOption);
public System.Data.Objects.ObjectResult<T> Execute (System.Data.Objects.MergeOption mergeOption);
override this.Execute : System.Data.Objects.MergeOption -> System.Data.Objects.ObjectResult<'T>
Public Function Execute (mergeOption As MergeOption) As ObjectResult(Of T)

參數

mergeOption
MergeOption

要在執行查詢時使用的 MergeOptionThe MergeOption to use when executing the query. 預設為 AppendOnlyThe default is AppendOnly.

傳回

ObjectResult<T>

ObjectResult<T>,其中包含查詢所傳回之實體 (Entity) 物件的集合。An ObjectResult<T> that contains a collection of entity objects returned by the query.

範例

這個範例是根據 Microsoft SQL Server 產品範例: DatabaseThis example is based on the Microsoft SQL Server Product Samples: Database. 此範例會從 ObjectResult<T> 方法傳回 ExecuteThe example returns an ObjectResult<T> from 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;
    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

備註

物件查詢的預設合併選項是 AppendOnlyThe default merge option for object queries is AppendOnly. 如需詳細資訊,請參閱 識別解析、狀態管理及變更追蹤For more information, see Identity Resolution, State Management, and Change Tracking.

適用於