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

定義

結合此查詢的結果與另一個物件查詢的結果,而且包括所有重複項目。Combines the results of the query with the results of another object query, including all duplicates.

public:
 System::Data::Objects::ObjectQuery<T> ^ UnionAll(System::Data::Objects::ObjectQuery<T> ^ query);
public System.Data.Objects.ObjectQuery<T> UnionAll (System.Data.Objects.ObjectQuery<T> query);
member this.UnionAll : System.Data.Objects.ObjectQuery<'T> -> System.Data.Objects.ObjectQuery<'T>
Public Function UnionAll (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 ALL 以便加入指定之 query 之結果的原始執行個體。A new ObjectQuery<T> instance that is equivalent to the original instance with UNION ALL applied to add the results of the specified query.

例外狀況

query 參數為 nullThe query parameter is null.

範例

本主題中的範例是以 Microsoft SQL Server 產品範例: Database為基礎。The example in this topic is based on the Microsoft SQL Server Product Samples: Database. 此範例會使用 UnionAll 方法來建立新的 ObjectQuery<T> 物件。The example uses the UnionAll method to create a new ObjectQuery<T> object. 然後,它會針對新的 Distinct 物件呼叫 ObjectQuery<T> 方法,以便取得這個查詢的唯一結果。Then it calls the Distinct method on the new ObjectQuery<T> object to get the unique results of this query.

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.UnionAll(productQuery2);

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

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

    // Iterate through the collection of Product items,
    // after the UnionAll method was called on two queries.
    foreach (Product result in productQuery3)
    {
        Console.WriteLine("Product Name: {0}", result.ProductID);
    }
    ObjectQuery<Product> productQuery4 = productQuery3.Distinct();

    Console.WriteLine("\nResult of Distinct");
    Console.WriteLine("------------------");

    // Iterate through the collection of Product items.
    // after the Distinct method was called on a query.
    foreach (Product result in productQuery4)
        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.UnionAll(productQuery2)

    productQuery3.Parameters.Add(New ObjectParameter("productID", productID))

    Console.WriteLine("Result of UnionAll")
    Console.WriteLine("------------------")

    ' Iterate through the collection of Product items, 
    ' after the UnionAll method was called on two queries. 
    For Each result As Product In productQuery3
        Console.WriteLine("Product Name: {0}", result.ProductID)
    Next
    Dim productQuery4 As ObjectQuery(Of Product) = productQuery3.Distinct()

    Console.WriteLine(vbLf & "Result of Distinct")
    Console.WriteLine("------------------")

    ' Iterate through the collection of Product items. 
    ' after the Distinct method was called on a query. 
    For Each result As Product In productQuery4
        Console.WriteLine("Product Name: {0}", result.ProductID)
    Next
End Using

備註

UnionAll 加入提供的結果 query (包括所有重複項)。UnionAll adds the results of the supplied query including all duplicates.

提供的 query ,定義要加入的結果必須是相同類型或可升級為這個類型的類型 ObjectQuery<T>The supplied query that defines 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>UnionAll 執行個體繼承連接。The resulting query inherits the connection from the ObjectQuery<T> instance on which UnionAll was called.

適用於