Questo articolo è stato tradotto automaticamente. Per visualizzare l'articolo in inglese, selezionare la casella di controllo Inglese. È possibile anche visualizzare il testo inglese in una finestra popup posizionando il puntatore del mouse sopra il testo.
Traduzione
Inglese

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

 

Combina i risultati della query con i risultati di un'altra query di oggetto inclusi tutti i duplicati.

Spazio dei nomi:   System.Data.Objects
Assembly:  System.Data.Entity (in System.Data.Entity.dll)

public ObjectQuery<T> UnionAll(
	ObjectQuery<T> query
)

Parametri

query
Type: System.Data.Objects.ObjectQuery<T>

Oggetto ObjectQuery<T> che rappresenta i risultati da aggiungere.

Valore restituito

Type: System.Data.Objects.ObjectQuery<T>

Nuova istanza di ObjectQuery<T> equivalente all'istanza originale con l'operatore UNION ALL applicato per aggiungere i risultati del parametro query specificato.

Exception Condition
ArgumentNullException

Il valore del parametro query è null.

UnionAll adds the results of the supplied query including all duplicates.

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 T:System.Data.Objects.ObjectQuery`1. For example, the following is valid because DiscontinuedProducts can be promoted to Products:

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

The following will throw an exception because Products cannot be promoted to DiscontinuedProducts.

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

For an T:System.Data.Objects.ObjectQuery`1 of type T:System.Data.Common.DbDataRecord, the records in both queries must have the same number of columns, and the types in the columns of the T:System.Data.Common.DbDataRecord of the passed query must be promotable to the types of the columns in the T:System.Data.Common.DbDataRecord of the T:System.Data.Objects.ObjectQuery`1.

Parameters that are defined in the supplied query are merged with parameters that are defined in the T:System.Data.Objects.ObjectQuery`1 instance. Parameters must be unique in the combined T:System.Data.Objects.ObjectParameterCollection. There cannot be two parameters in the combined collection with the same name. For more information, see Query Builder Methods.

The resulting query inherits the connection from the T:System.Data.Objects.ObjectQuery`1 instance on which M:System.Data.Objects.ObjectQuery`1.UnionAll(System.Data.Objects.ObjectQuery{`0}) was called.

The example in this topic is based on the AdventureWorks Sales Model. The example uses the M:System.Data.Objects.ObjectQuery`1.UnionAll(System.Data.Objects.ObjectQuery{`0}) method to create a new T:System.Data.Objects.ObjectQuery`1 object. Then it calls the M:System.Data.Objects.ObjectQuery`1.Distinct method on the new T:System.Data.Objects.ObjectQuery`1 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);
}

.NET Framework
Disponibile da 3.5
Torna all'inizio
Mostra: