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> 实例,等效于应用了 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 参数为 null。The query parameter is null.
示例
此示例基于 Microsoft SQL Server 产品示例:数据库。This 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 可以升级到 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>)
对于 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.