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)

반환

원래 인스턴스에 SELECT DISTINCT가 적용된 것과 동일한 새 ObjectQuery<T> 인스턴스입니다.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).

적용 대상

추가 정보