ObjectQuery<T>.Distinct Metod

Definition

Begränsar frågan till unika resultat.

public:
 System::Data::Objects::ObjectQuery<T> ^ Distinct();
public System.Data.Objects.ObjectQuery<T> Distinct();
member this.Distinct : unit -> System.Data.Objects.ObjectQuery<'T>
Public Function Distinct () As ObjectQuery(Of T)

Returer

En ny ObjectQuery<T> instans som motsvarar den ursprungliga instansen med SELECT DISTINCT tillämpad.

Exempel

I det här exemplet används UnionAll metoden för att skapa ett nytt ObjectQuery<T> objekt. Sedan anropas Distinct 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

Den här frågeverktyget returnerar en ObjectQuery<T> instans som motsvarar den ursprungliga frågan med SELECT DISTINCT tillämpad.

Operatorn DISTINCT kan inte tillämpas på ett objekt som innehåller en mappning till en icke-jämförbar kolumn i datakällan (till exempel ntext).

Gäller för

Se även