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

Definition

Kombinerar resultatet av frågan med resultatet av en annan objektfråga, inklusive alla dubbletter.

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)

Parametrar

query
ObjectQuery<T>

En ObjectQuery<T> som representerar de resultat som ska läggas till.

Returer

En ny ObjectQuery<T> instans som motsvarar den ursprungliga instansen med UNION ALL tillämpad för att lägga till resultatet av den angivna query.

Undantag

Parametern query är null.

Exempel

I det UnionAll här exemplet används metoden för att skapa ett nytt ObjectQuery<T> objekt. Sedan anropas Distinct metoden för det nya ObjectQuery<T> objektet för att få det unika resultatet av den här frågan.

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);
}

Kommentarer

UnionAll lägger till resultatet av de angivna query , inklusive alla dubbletter.

Den angivna query som definierar resultat som ska läggas till måste vara av samma typ eller av en typ som kan höjas upp till typen av detta ObjectQuery<T>. Följande är till exempel giltigt eftersom DiscontinuedProducts kan höjas till Products:

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

Följande utlöser ett undantag eftersom Products det inte går att höja upp till DiscontinuedProducts.

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

För en ObjectQuery<T> typ DbDataRecordmåste posterna i båda frågorna ha samma antal kolumner, och typerna i kolumnerna DbDataRecord i den skickade query måste vara promotable till typerna av kolumnerna i DbDataRecord i ObjectQuery<T>.

Parametrar som definieras i den angivna query slås samman med parametrar som definieras i instansen ObjectQuery<T> . Parametrarna måste vara unika i den kombinerade ObjectParameterCollection. Det kan inte finnas två parametrar i den kombinerade samlingen med samma namn. Mer information finns i Query Builder-metoder.

Den resulterande frågan ärver anslutningen från den ObjectQuery<T> instans som UnionAll anropades.

Gäller för

Se även