다음을 통해 공유


방법: LINQ to Entities 쿼리 결과에서 중복 요소 제거

이 항목에서는 Distinct를 사용하여 쿼리 결과에서 중복 요소를 제거하는 방법에 대한 예제를 제공합니다.

이 항목의 예제는 Adventure Works Sales 모델을 기반으로 합니다. 이 항목의 코드를 실행하려면 프로젝트에 Adventure Works Sales 모델을 추가하고 프로젝트에서 Entity Framework를 사용하도록 구성해야 합니다. 자세한 내용은 방법: 엔터티 데이터 모델 마법사 사용(Entity Framework) 또는 방법: Entity Framework 프로젝트 수동 구성방법: 엔터티 데이터 모델 수동 정의(Entity Framework)를 참조하십시오.

예제

설명

이 예제에서는 Distinct 메서드를 사용하여 고유한 성을 반환합니다.

코드

Using context As New AdventureWorksEntities()
    Dim contacts = context.Contacts

    Dim contactsQuery = _
        From c In contacts _
        Select c.LastName

    Dim distinctNames = contactsQuery.Distinct()

    For Each name In distinctNames
        Console.WriteLine("Name: " + name)
    Next
End Using
using (AdventureWorksEntities context = new AdventureWorksEntities())
{
    IQueryable<string> contactsQuery = from c in context.Contacts
                        select c.LastName;

    IQueryable<string> distinctNames = contactsQuery.Distinct();

    foreach (string name in distinctNames)
    {
        Console.WriteLine("Name: {0}", name);
    }
}

참고 항목

개념

개념적 모델 쿼리(Entity Framework)