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

Definición

Combina los resultados de la consulta con los resultados de otra consulta de objeto, pero sin duplicados.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)

Parámetros

query
ObjectQuery<T> ObjectQuery<T> ObjectQuery<T> ObjectQuery<T>

ObjectQuery<T> que representa los resultados que se van a agregar.An ObjectQuery<T> that represents the results to add.

Devoluciones

Nueva instancia de ObjectQuery<T> que es equivalente a la instancia original, pero aplicando UNION para agregar los resultados de la query especificada.A new ObjectQuery<T> instance that is equivalent to the original instance with UNION applied to add the results of the specified query.

Excepciones

El parámetro query es null.The query parameter is null.

Ejemplos

En este ejemplo se basa en el modelo AdventureWorks Sales.This example is based on the AdventureWorks Sales Model. En el ejemplo, se usa el método Union para crear un objeto ObjectQuery<T> nuevo.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

Comentarios

Union Agrega los resultados de la query sin ningún duplicado.Union adds the results of the supplied query without any duplicates.

Suministrado query que define los resultados que se va a agregar debe ser del mismo tipo o de un tipo que se puede promover al tipo de este 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>. Por ejemplo, la siguiente es válida porque DiscontinuedProducts se puede promover a Products:For example, the following is valid because DiscontinuedProducts can be promoted to Products:

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

La siguiente generará una excepción porque Products no se puede promover a DiscontinuedProducts.The following will throw an exception because Products cannot be promoted to DiscontinuedProducts.

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

Para un ObjectQuery<T> de tipo DbDataRecord, los registros de ambas consultas deben tener el mismo número de columnas y los tipos en las columnas de la DbDataRecord del pasado query debe ser promovibles a los tipos de las columnas de la DbDataRecord de la 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>.

Parámetros que se definen en proporcionado query se combinan con los parámetros que se definen en el ObjectQuery<T> instancia.Parameters that are defined in the supplied query are merged with parameters that are defined in the ObjectQuery<T> instance. Los parámetros deben ser únicos en la ObjectParameterCollection combinada.Parameters must be unique in the combined ObjectParameterCollection. No puede haber dos parámetros en la colección combinada con el mismo nombre.There cannot be two parameters in the combined collection with the same name. Para obtener más información, consulte métodos del generador de consultas.For more information, see Query Builder Methods.

La consulta resultante hereda la conexión de la instancia de ObjectQuery<T> en la que se llamó a Union.The resulting query inherits the connection from the ObjectQuery<T> instance on which Union was called.

Se aplica a

Consulte también: