ObjectQuery<T>.UnionAll(ObjectQuery<T>) ObjectQuery<T>.UnionAll(ObjectQuery<T>) ObjectQuery<T>.UnionAll(ObjectQuery<T>) ObjectQuery<T>.UnionAll(ObjectQuery<T>) Method

定义

将查询结果与另一个对象查询的结果进行组合(包括所有重复项)。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> ObjectQuery<T> ObjectQuery<T>

一个 ObjectQuery<T>,表示要添加的结果。An ObjectQuery<T> that represents the results to add.

返回

新的 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.

示例

本主题中的示例基于AdventureWorks 销售模型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可以升级到: 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>)   

DbDataRecord DbDataRecord query对于类型ObjectQuery<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. 组合后的集合中不能有两个具有相同名称的参数。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.

适用于

另请参阅