Bagikan melalui


ObjectQuery<T>.Distinct Metode

Definisi

Membatasi kueri ke hasil unik.

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)

Mengembalikan

Instans baru ObjectQuery<T> yang setara dengan instans asli dengan SELECT DISTINCT diterapkan.

Contoh

Contoh ini menggunakan UnionAll metode untuk membuat objek baru ObjectQuery<T> . Kemudian memanggil Distinct objek baru ObjectQuery<T> untuk mendapatkan hasil unik dari kueri ini.

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

Keterangan

Metode penyusun kueri ini mengembalikan instans ObjectQuery<T> yang setara dengan kueri asli dengan SELECT DISTINCT yang diterapkan.

Operator DISTINCT tidak dapat diterapkan ke objek yang menyertakan pemetaan ke kolom yang tidak sebanding di sumber data (seperti ntext).

Berlaku untuk

Lihat juga