Export (0) Print
Expand All

ObjectQuery(Of T).Union Method

Combines the results of the query with the results of another object query, without any duplicates.

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

Public Function Union ( _
	query As ObjectQuery(Of T) _
) As ObjectQuery(Of T)


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

An ObjectQuery(Of T) that represents the results to add.

Return Value

Type: System.Data.Objects.ObjectQuery(Of T)
A new ObjectQuery(Of T) instance that is equivalent to the original instance with UNION applied to add the results of the specified query.


The query parameter is Nothing.

Union adds the results of the supplied query without any duplicates.

The supplied query that defines the results to add must be of the same type or of a type that can be promoted to the type of this ObjectQuery(Of T). For example, the following is valid because DiscontinuedProducts can be promoted to Products:


The following will throw an exception because Products cannot be promoted to DiscontinuedProducts.

ObjectQuery <DiscontinuedProduct>.Union(ObjectQuery<Product>) 

For an ObjectQuery(Of T) of type DbDataRecord, the records in both queries must have the same number of columns, and the types in the columns of the DbDataRecord of the passed query must be promotable to the types of the columns in the DbDataRecord of the ObjectQuery(Of T).

Parameters that are defined in the supplied query are merged with parameters that are defined in the ObjectQuery(Of T) instance. Parameters must be unique in the combined ObjectParameterCollection. There cannot be two parameters in the combined collection with the same name. For more information, see Query Builder Methods (Entity Framework).

The resulting query inherits the connection from the ObjectQuery(Of T) instance on which Union was called.

This example is based on the Adventure Works Sales Model. The example uses Union method to creates a new ObjectQuery(Of T) object.

Dim productID = 100
Using context As New AdventureWorksEntities()
    Dim queryString As String = "SELECT VALUE product FROM AdventureWorksEntities.Products AS product " & _
            " WHERE product.ProductID < @productID" 
    Dim productQuery As New ObjectQuery(Of Product)(queryString, context, MergeOption.NoTracking)

    Dim productQuery2 As New ObjectQuery(Of Product)(queryString, context, MergeOption.NoTracking)

    Dim productQuery3 As ObjectQuery(Of Product) = productQuery.Union(productQuery2)

    productQuery3.Parameters.Add(New ObjectParameter("productID", productID))

    Console.WriteLine("Result of Union")

    ' Iterate through the collection of Product items,  
    ' after the Union method was called on two queries.  
    For Each result As Product In productQuery3
        Console.WriteLine("Product Name: {0}", result.ProductID)
End Using

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5 SP1

.NET Framework Client Profile

Supported in: 4

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

© 2014 Microsoft