TechTalk Genome v4.2

Union

set1 + [with(UnionMethod=union-hint)] set2
set1
An OQL expression evaluating to a Set<T> of scalar or persistent objects.
set2
An OQL expression evaluating to a Set<T> of scalar or persistent objects.
union-hint
A value indicating the union query building method to be applied by Genome. A value of UnionMethod.Union instructs the query engine to conduct a UNION query; A value of UnionMethod.CrossFilter instructs the query engine to conduct a cross-filtered union query. The value of this modifier only has effects on query execution performance and may be ignored by the query engine if the specified behaviour is not applicable in certain scenarios.

Return Value

A Set<T> containing all elements of set1 and set2.

Remarks

The union operator returns a Set<T> containing all elements of set1 and set2. The element type of the resulting set is the most specialised common type of the element type of set1 and set2 (the current implementation restricts set1 and set2 to having the same element type, hence the element type of the resulting Set<T> is identical to the element type of both source sets).

The query used to enumerate the elements of the resulting Set<T> can be conducted in multiple ways by the query engine. If set1 and set2 have the same element type and both have the same or no projections applied, then the resulting set is defined by the query

    [projection]extentof(T)[filter1 || filter2] 

where projection is the same projection applied to both sets, T is the element type of the source sets, filter1 is the filter criteria (if any) applied to set1 and filter2 is the filter criteria applied to set2. This method of conducting the resulting query is referred to as cross-filtering set1 with set2.

The query

    [Age]extentof(Person)[Name=="John Doe"] + [Age]extentof(Person)[Name=="Foo Bar"]  

translates to

    SELECT Age FROM Person WHERE Name='John Doe' OR Name='Foo Bar'  

The query engine always tries to produce cross-filtered unions wherever possible, unless otherwise directed by union-hint.

If set1 and set2 are of the same element type but the conditions defined above do not apply, a standard T-SQL UNION query is generated if the underlying relational database server supports it.

Hence, the following OQL expression

    [Age]extentof(Person)[Age > 25] + [Age]extentof(CompanyCar)[Age < 5]  

translates to

    (SELECT Age FROM Person WHERE Age > 25) UNION (SELECT Age FROM CompanyCar WHERE Age < 5)  

Quick Reference

OQL
extentof(Animal)[Age > 10] + extentof(Animal)[Age < 1]


extentof(Animal)[Age > 10] + with(UnionMethod=UnionMethod.Union) extentof(Animal)[Age < 1]

C#
Set<Animal> animals =
myDB.Extent<Animal>()["Age > {0}", 10] +
myDB.Extent<Animal>()["Age < {0}", 1];


Set<Animal> animals = Set<Animal>.Union(
myDB.Extent<Animal>()["Age > {0}", 10],
myDB.Extent<Animal>()["Age < {0}", 1],
UnionMethod.Union);

SQL
SELECT * FROM Animal WHERE Age < 1 OR Age > 10


SELECT * FROM Animal WHERE Age < 1
UNION
SELECT * FROM Animal WHERE Age > 10

See Also

Set<T>.op_Addition | Set<T>.Union | OqlReference.Chapter11