UNION (Entity SQL)UNION (Entity SQL)

將二個或多個查詢的結果結合成單一集合。Combines the results of two or more queries into a single collection.

語法Syntax

expression  
UNION [ ALL ]  
expression  

引數Arguments

expression
任何有效的查詢運算式,該運算式會傳回與此集合結合的集合。所有運算式都必須具有與 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.

UNIONUNION
指定要結合多個集合,並當做單一集合傳回。Specifies that multiple collections are to be combined and returned as a single collection.

ALLALL
指定要結合多個集合,並當做單一集合傳回,包括重複的項目。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

具有與 expression相同的型別或是共同基底類型或衍生型別的集合。A collection of the same type or of a common base or derived type as expression.

備註Remarks

UNION 是其中一個 Entity SQLEntity SQL 設定運算子。UNION is one of the Entity SQLEntity SQL set operators. 所有 Entity SQLEntity SQL 設定運算子都會從左到右評估。All Entity SQLEntity SQL set operators are evaluated from left to right. 如需設定運算子的優先順序資訊 Entity SQLEntity SQL ,請參閱 EXCEPTFor precedence information for the Entity SQLEntity SQL set operators, see EXCEPT.

範例Example

下列 Entity SQL 查詢會使用 UNION ALL 運算子,將兩個查詢的結果結合成單一集合。The following Entity SQL query uses the UNION ALL operator to combine the results of two queries into a single collection. 此查詢是根據 AdventureWorks Sales Model。The query is based on the AdventureWorks Sales Model. 若要編譯及執行此查詢,請遵循以下步驟:To compile and run this query, follow these steps:

  1. 遵循 How to: Execute a Query that Returns StructuralType Results中的程序進行。Follow the procedure in How to: Execute a Query that Returns StructuralType Results.

  2. 將下列查詢當成引數,傳遞至 ExecuteStructuralTypeQuery 方法:Pass the following query as an argument to the ExecuteStructuralTypeQuery method:

(SELECT VALUE P FROM AdventureWorksEntities.Products 
    AS P WHERE P.Name LIKE 'C%') UNION ALL 
    (SELECT VALUE A FROM AdventureWorksEntities.Products 
    AS A WHERE A.ListPrice > @price)

另請參閱See also