방법: 데이터 정렬(Entity Framework)

이 항목에서는 쿼리 결과를 정렬하는 방법을 보여 줍니다. 이 예제에서는 Contact.LastName의 첫 번째 문자를 기준으로 사전순으로 정렬된 Contact 개체 컬렉션을 반환합니다. 다음의 Entity Framework 쿼리 기술을 각각 사용하여 동일한 예제를 보여 줍니다.

  • LINQ to Entities

  • ObjectQuery<T>가 포함된 Entity SQL

  • ObjectQuery<T>의 쿼리 작성기 메서드

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

예제

다음은 LINQ to Entities 예제입니다.

Using context As New AdventureWorksEntities()
    ' Define a query that returns a list 
    ' of Contact objects sorted by last name. 
    Dim sortedNames = From n In context.Contacts _
        Order By n.LastName _
        Select n

    Console.WriteLine("The sorted list of last names:")
    For Each name As Contact In sortedNames
        Console.WriteLine(name.LastName)
    Next
End Using
using (AdventureWorksEntities context =
    new AdventureWorksEntities())
{
    // Define a query that returns a list 
    // of Contact objects sorted by last name.
    var sortedNames =
        from n in context.Contacts
        orderby n.LastName
        select n;

    Console.WriteLine("The sorted list of last names:");
    foreach (Contact name in sortedNames)
    {
        Console.WriteLine(name.LastName);
    }
}

다음은 Entity SQL 예제입니다.

' Define the Entity SQL query string that returns 
' Contact objects sorted by last name. 
Dim queryString As String = "SELECT VALUE contact FROM Contacts AS contact Order By contact.LastName"

Using context As New AdventureWorksEntities()
    ' Define an ObjectQuery that returns a collection 
    ' of Contact objects sorted by last name. 
    Dim query As New ObjectQuery(Of Contact)(queryString, context)

    Console.WriteLine("The sorted list of last names:")
    For Each name As Contact In query.Execute(MergeOption.AppendOnly)
        Console.WriteLine(name.LastName)
    Next
End Using
// Define the Entity SQL query string that returns 
// Contact objects sorted by last name.
string queryString = @"SELECT VALUE contact FROM Contacts AS contact 
        Order By contact.LastName";

using (AdventureWorksEntities context =
    new AdventureWorksEntities())
{
    // Define an ObjectQuery that returns a collection 
    // of Contact objects sorted by last name.
    ObjectQuery<Contact> query =
        new ObjectQuery<Contact>(queryString, context);

    Console.WriteLine("The sorted list of last names:");
    foreach (Contact name in query.Execute(MergeOption.AppendOnly))
    {
        Console.WriteLine(name.LastName);
    }
}

다음은 쿼리 작성기 메서드 예제입니다.

Using context As New AdventureWorksEntities()
    ' Define an ObjectQuery that returns a collection 
    ' of Contact objects sorted by last name. 
    Dim query As ObjectQuery(Of Contact) = context.Contacts.OrderBy("it.LastName")

    Console.WriteLine("The sorted list of last names:")
    For Each name As Contact In query.Execute(MergeOption.AppendOnly)
        Console.WriteLine(name.LastName)
    Next
End Using
using (AdventureWorksEntities context =
    new AdventureWorksEntities())
{
    // Define an ObjectQuery that returns a collection 
    // of Contact objects sorted by last name.
    ObjectQuery<Contact> query =
        context.Contacts.OrderBy("it.LastName");

    Console.WriteLine("The sorted list of last names:");
    foreach (Contact name in query.Execute(MergeOption.AppendOnly))
    {
        Console.WriteLine(name.LastName);
    }
}

참고 항목

개념

개념적 모델 쿼리(Entity Framework)