ObjectQuery<'T>.Union Method (ObjectQuery<'T>)


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)

member Union : 
        query:ObjectQuery<'T> -> ObjectQuery<'T>


Type: System.Data.Objects.ObjectQuery<'T>

An ObjectQuery<'T> that represents the results to add.

Return Value

Type: System.Data.Objects.ObjectQuery<'T>

A new ObjectQuery<'T> instance that is equivalent to the original instance with UNION applied to add the results of the specified query.

Exception Condition

The query parameter is null.

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<'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<'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<'T>.

Parameters that are defined in the supplied query are merged with parameters that are defined in the ObjectQuery<'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.

The resulting query inherits the connection from the ObjectQuery<'T> instance on which Union was called.

This example is based on the AdventureWorks Sales Model. The example uses Union method to creates a new ObjectQuery<'T> object.

No code example is currently available or this language may not be supported.

.NET Framework
Available since 3.5
Return to top