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.

戻り値

指定された query の結果を追加するために UNION ALL を適用した元のインスタンスに相当する、新しい ObjectQuery<T> インスタンス。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 パラメーターが null です。The query parameter is null.

このトピックの例は、 AdventureWorks Sales Modelに基づいています。The example in this topic is based on the AdventureWorks Sales Model. この例では、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 Products昇格させることができるので有効です。For example, the following is valid because DiscontinuedProducts can be promoted to Products:

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

をに昇格することはできProductsないため、次DiscontinuedProductsのような例外がスローされます。The following will throw an exception because Products cannot be promoted to DiscontinuedProducts.

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

query DbDataRecord DbDataRecordObjectQuery<T> のの場合、両方のクエリのレコードの列数は同じである必要があります。また、渡されたのの列の型は、の内の列の型に上位変換可能である必要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. 結合されたコレクション内に同じ名前のパラメーターが 2 つ存在することはできません。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.

適用対象

こちらもご覧ください