Condividi tramite


UNION (Entity SQL)

Combines the results of two or more queries into a single collection.

expression
UNION [ ALL ]
expression

Arguments

  • expression
    Any valid query expression that returns a collection to combine with the collection All expressions must be of the same type or of a common base or derived type as expression.
  • UNION
    Specifies that multiple collections are to be combined and returned as a single collection.
  • ALL
    Specifies that multiple collections are to be combined and returned as a single collection, including duplicates. If not specified, duplicates are removed from the result collection.

Return Value

A collection of the same type or of a common base or derived type as expression.

Remarks

UNION is one of the Entity SQL set operators. All Entity SQL set operators are evaluated from left to right. For precedence information for the Entity SQL set operators, see EXCEPT (Entity SQL).

Example

The following Entity SQL query uses the UNION ALL operator to combine the results of two queries into a single collection. The query is based on the AdventureWorks Sales Model. To compile and run this query, follow these steps:

  1. Follow the procedure in How to: Execute a Query that Returns StructuralType Results (EntityClient).

  2. Pass the following query as an argument to the ExecuteStructuralTypeQuery method:

(select VALUE P from AdventureWorksEntities.Product 
    as P WHERE P.Name LIKE 'C%') Union All 
    ( select VALUE A from AdventureWorksEntities.Product 
    as A where A.ListPrice > 20)

The output is shown below:

ProductID: 320
Name: Chainring Bolts
ProductNumber: CB-2903
MakeFlag: False
ProductID: 321
Name: Chainring Nut
ProductNumber: CN-6137
MakeFlag: False
ProductID: 322
Name: Chainring
ProductNumber: CR-7833
MakeFlag: False
ProductID: 323
Name: Crown Race
ProductNumber: CR-9981
MakeFlag: False
ProductID: 324
Name: Chain Stays
ProductNumber: CS-2812
MakeFlag: True
...

See Also

Concepts

Entity SQL Reference

Other Resources

Set Operators