다음을 통해 공유


쿼리 식 구문 예제: 정렬(LINQ to Entities)

이 항목의 예제에서는 쿼리 식 구문을 사용하여 AdventureWorks Sales 모델을 쿼리하기 위해 OrderByOrderByDescending 메서드를 사용하는 방법을 보여 줍니다. 이 예제에서 사용하는 AdventureWorks Sales 모델은 AdventureWorks 샘플 데이터베이스의 Contact, Address, Product, SalesOrderHeader 및 SalesOrderDetail 테이블로 작성되었습니다.

이 항목의 예제에서는 다음 using/Imports 문을 사용합니다.

Option Explicit On
Option Strict On
Imports System.Data.Objects
Imports System.Globalization
using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Objects;
using System.Globalization;
using System.Data.EntityClient;
using System.Data.SqlClient;
using System.Data.Common;

OrderBy

예제

다음 예제에서는 OrderBy를 사용하여 성을 기준으로 정렬된 연락처 목록을 반환합니다.

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

    Dim sortedContacts = _
        From contact In contacts _
        Order By contact.LastName _
        Select contact

    Console.WriteLine("The sorted list of last names:")
    For Each n As Contact In sortedContacts
        Console.WriteLine(n.LastName)
    Next
End Using
using (AdventureWorksEntities context = new AdventureWorksEntities())
{
    IQueryable<Contact> sortedNames =
        from n in context.Contacts
        orderby n.LastName
        select n;

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

예제

다음 예제에서는 OrderBy를 사용하여 성의 길이를 기준으로 연락처 목록을 정렬합니다.

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

    Dim sortedNames = _
        From n In contacts _
        Order By n.LastName.Length _
        Select n

    Console.WriteLine("The sorted list of last names (by length):")
    For Each n As Contact In sortedNames
        Console.WriteLine(n.LastName)
    Next
End Using
using (AdventureWorksEntities context = new AdventureWorksEntities())
{
    IQueryable<Contact> sortedNames =
        from n in context.Contacts
        orderby n.LastName.Length
        select n;

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

OrderByDescending

예제

다음 예제에서는 OrderByDescending 메서드와 같은 orderby… descending(Visual Basic에서는 Order By … Descending)을 사용하여 가격 목록을 내림차순으로 정렬합니다.

Using context As New AdventureWorksEntities
    Dim products As ObjectSet(Of Product) = context.Products

    Dim sortedPrices = _
        From product In products _
        Order By product.ListPrice Descending _
        Select product.ListPrice

    Console.WriteLine("The list price from highest to lowest:")
    For Each price As Decimal In sortedPrices
        Console.WriteLine(price)
    Next
End Using
using (AdventureWorksEntities context = new AdventureWorksEntities())
{
    IQueryable<Decimal> sortedPrices =
        from p in context.Products
        orderby p.ListPrice descending
        select p.ListPrice;

    Console.WriteLine("The list price from highest to lowest:");
    foreach (Decimal price in sortedPrices)
    {
        Console.WriteLine(price);
    }
}

ThenBy

예제

다음 예제에서는 OrderByThenBy를 사용하여 성을 기준으로 정렬한 다음 이름을 기준으로 다시 정렬한 연락처 목록을 반환합니다.

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

    Dim sortedContacts = _
        From contact In contacts _
        Order By contact.LastName, contact.FirstName _
        Select contact

    Console.WriteLine("The list of contacts sorted by last name then by first name:")
    For Each sortedContact As Contact In sortedContacts
        Console.WriteLine(sortedContact.LastName + ", " + sortedContact.FirstName)
    Next
End Using
using (AdventureWorksEntities context = new AdventureWorksEntities())
{
    IQueryable<Contact> sortedContacts =
        from contact in context.Contacts
        orderby contact.LastName, contact.FirstName
        select contact;

    Console.WriteLine("The list of contacts sorted by last name then by first name:");
    foreach (Contact sortedContact in sortedContacts)
    {
        Console.WriteLine(sortedContact.LastName + ", " + sortedContact.FirstName);
    }
}

ThenByDescending

예제

다음 예제에서는 ThenByDescending 메서드와 같은 OrderBy… Descending을 사용하여 이름과 가격을 각각 1차 및 2차 정렬 기준으로 삼아 내림차순으로 제품 목록을 정렬합니다.

Using context As New AdventureWorksEntities
    Dim products As ObjectSet(Of Product) = context.Products

    Dim query As IQueryable(Of Product) = _
        From product In products _
        Order By product.Name, product.ListPrice Descending _
        Select product

    For Each prod As Product In query
        Console.WriteLine("Product ID: {0} Product Name: {1} List Price {2}", _
            prod.ProductID, _
            prod.Name, _
            prod.ListPrice)
    Next
End Using
using (AdventureWorksEntities context = new AdventureWorksEntities())
{
    IQueryable<Product> query =
        from product in context.Products
        orderby product.Name, product.ListPrice descending
        select product;

    foreach (Product product in query)
    {
        Console.WriteLine("Product ID: {0} Product Name: {1} List Price {2}",
            product.ProductID,
            product.Name,
            product.ListPrice);
    }
}

참고 항목

개념

LINQ to Entities의 쿼리