ObjectQuery<T>.Distinct ObjectQuery<T>.Distinct ObjectQuery<T>.Distinct ObjectQuery<T>.Distinct Method

Определение

Ограничивает запрос для выдачи результатов без повторов.Limits the query to unique results.

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)

Возвраты

Новый экземпляр запроса ObjectQuery<T>, эквивалентный первоначальному экземпляру с примененным к нему предложением SELECT DISTINCT.A new ObjectQuery<T> instance that is equivalent to the original instance with SELECT DISTINCT applied.

Примеры

Пример в этом разделе основан на модели AdventureWorks Sales.The example in this topic is based on the AdventureWorks Sales Model. В следующем примере используется метод UnionAll для создания нового объекта ObjectQuery<T>.The example uses UnionAll method to create a new ObjectQuery<T> object. Затем к новому объекту Distinct применяется ключевое слово ObjectQuery<T> для получения неповторяющихся результатов запроса.Then it calls Distinct 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

Комментарии

Этот метод построителя запросов возвращает ObjectQuery<T> экземпляра, эквивалентный исходному запросу с использованием SELECT DISTINCT применения.This query builder method returns an ObjectQuery<T> instance that is equivalent to the original query with SELECT DISTINCT applied.

DISTINCT Оператор не может применяться к объекту, который включает в себя сопоставление с несравнимым столбцом в источнике данных (например, типа ntext).The DISTINCT operator cannot be applied to an object that includes a mapping to a non-comparable column in the data source (such as ntext).

Применяется к

Дополнительно