ObjectQuery(Of T).Execute Method (MergeOption)


The .NET API Reference documentation has a new home. Visit the .NET API Browser on docs.microsoft.com to see the new experience.

Executes the object query with the specified merge option.

Namespace:   System.Data.Objects
Assembly:  System.Data.Entity (in System.Data.Entity.dll)

Public Function Execute (
	mergeOption As MergeOption
) As ObjectResult(Of T)


Type: System.Data.Objects.MergeOption

The MergeOption to use when executing the query. The default is AppendOnly.

Return Value

Type: System.Data.Objects.ObjectResult(Of T)

An ObjectResult(Of T) that contains a collection of entity objects returned by the query.

The default merge option for object queries is AppendOnly. For more information, see Identity Resolution, State Management, and Change Tracking.

This example is based on the AdventureWorks Sales Model. The example returns an ObjectResult(Of T) from Execute method. Then it gets an enumerator and iterates through the query results. At the end it releases the enumerator and the ObjectResult(Of T) object.

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

.NET Framework
Available since 3.5
Return to top