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

Definición

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

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 ALL para agregar los resultados de la query especificada.A new ObjectQuery<T> instance that is equivalent to the original instance with UNION ALL applied to add the results of the specified query.

Excepciones

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

Ejemplos

El ejemplo de este tema se basa en el modelo AdventureWorks Sales.The example in this topic is based on the AdventureWorks Sales Model. En el ejemplo, se usa el método UnionAll para crear un objeto ObjectQuery<T> nuevo.The example uses the UnionAll method to create a new ObjectQuery<T> object. A continuación, se llama al método Distinct en el nuevo objeto ObjectQuery<T> para obtener los resultados únicos de esta consulta.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

Comentarios

UnionAll Agrega los resultados de la query incluyendo todos los duplicados.UnionAll adds the results of the supplied query including all duplicates.

Suministrado query que define los resultados que 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 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 UnionAll.The resulting query inherits the connection from the ObjectQuery<T> instance on which UnionAll was called.

Se aplica a

Consulte también: