ObjectQuery<T>.Union(ObjectQuery<T>) 方法

定義

結合此查詢的結果與另一個物件查詢的結果,但不包含任何重複項目。Combines the results of the query with the results of another object query, without any duplicates.

public:
 System::Data::Objects::ObjectQuery<T> ^ Union(System::Data::Objects::ObjectQuery<T> ^ query);
public System.Data.Objects.ObjectQuery<T> Union (System.Data.Objects.ObjectQuery<T> query);
member this.Union : System.Data.Objects.ObjectQuery<'T> -> System.Data.Objects.ObjectQuery<'T>
Public Function Union (query As ObjectQuery(Of T)) As ObjectQuery(Of T)

參數

query
ObjectQuery<T>

ObjectQuery<T>,它代表要加入的結果。An ObjectQuery<T> that represents the results to add.

傳回

ObjectQuery<T>

新的 ObjectQuery<T> 執行個體,它就相當於套用了 UNION 以便加入指定之 query 之結果的原始執行個體。A new ObjectQuery<T> instance that is equivalent to the original instance with UNION applied to add the results of the specified query.

例外狀況

query 參數為 nullThe query parameter is null.

範例

這個範例是根據 Microsoft SQL Server 產品範例: DatabaseThis example is based on the Microsoft SQL Server Product Samples: Database. 此範例會使用 Union 方法來建立新的 ObjectQuery<T> 物件。The example uses Union method to creates a new ObjectQuery<T> object.

int productID = 100;
using (AdventureWorksEntities context =
    new AdventureWorksEntities())
{
    string queryString = @"SELECT VALUE product
        FROM AdventureWorksEntities.Products AS product
        WHERE product.ProductID < @productID";

    ObjectQuery<Product> productQuery =
        new ObjectQuery<Product>(queryString,
            context, MergeOption.NoTracking);

    ObjectQuery<Product> productQuery2 =
        new ObjectQuery<Product>(queryString,
            context, MergeOption.NoTracking);

    ObjectQuery<Product> productQuery3 =
        productQuery.Union(productQuery2);

    productQuery3.Parameters.Add(new ObjectParameter("productID", productID));

    Console.WriteLine("Result of Union");
    Console.WriteLine("------------------");

    // Iterate through the collection of Product items,
    // after the Union method was called on two queries.
    foreach (Product result in productQuery3)
    {
        Console.WriteLine("Product Name: {0}", result.ProductID);
    }
}
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")
    Console.WriteLine("------------------")

    ' 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)
    Next
End Using

備註

Union 加入提供的結果, query 但不含任何重複專案。Union adds the results of the supplied query without any duplicates.

提供的 query ,定義要加入的結果必須是相同類型或可升級為這個類型的類型 ObjectQuery<T>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>. 例如,以下是有效的,因為 DiscontinuedProducts 可以升級為 ProductsFor example, the following is valid because DiscontinuedProducts can be promoted to Products:

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

下列將擲回例外狀況,因為 Products 無法升級為 DiscontinuedProductsThe following will throw an exception because Products cannot be promoted to DiscontinuedProducts.

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

針對 ObjectQuery<T> 類型的 DbDataRecord ,這兩個查詢中的記錄都必須具有相同的資料行數目,而且所傳遞之資料行中的類型 DbDataRecord query 必須可提升為的資料行類型 DbDataRecord ObjectQuery<T>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>.

在提供的中定義的參數 query 會與實例中定義的參數合併 ObjectQuery<T>Parameters that are defined in the supplied query are merged with parameters that are defined in the ObjectQuery<T> instance. 參數在結合的 ObjectParameterCollection 中必須是唯一的。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.

結果查詢會從在上面呼叫 ObjectQuery<T>Union 執行個體繼承連接。The resulting query inherits the connection from the ObjectQuery<T> instance on which Union was called.

適用於