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

定义

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

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

返回

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

示例

此示例基于AdventureWorks 销售模型This example is based on the AdventureWorks Sales Model. 该示例使用 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>)   

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> 实例(调用了它的 Union)继承连接。The resulting query inherits the connection from the ObjectQuery<T> instance on which Union was called.

适用于

另请参阅