다음을 통해 공유


시퀀스의 요소 정렬

연산자를 OrderBy 사용하여 하나 이상의 키에 따라 시퀀스를 정렬합니다.

비고

LINQ to SQL은 간단한 기본 형식(예: stringint등)에 의한 순서 지정을 지원하도록 설계되었습니다. 익명 형식과 같은 복잡한 다중 값 클래스에 대한 순서 지정은 지원하지 않습니다. byte 데이터 형식을 지원하지 않습니다.

예제 1

다음 예제에서는 고용 날짜를 기준으로 정렬 Employees 합니다.

IOrderedQueryable<Employee> hireQuery =
    from emp in db.Employees
    orderby emp.HireDate
    select emp;

foreach (Employee empObj in hireQuery)
{
    Console.WriteLine("EmpID = {0}, Date Hired = {1}",
        empObj.EmployeeID, empObj.HireDate);
}
Dim hireQuery = _
    From emp In db.Employees _
    Select emp _
    Order By emp.HireDate

For Each empObj As Employee In hireQuery
    Console.WriteLine("EmpID = {0}, Date Hired = {1}", _
        empObj.EmployeeID, empObj.HireDate)
Next

예제 2

다음 예제는 where을 사용하여 Orders이 항공화물로 London에 배송된 항목을 정렬하는 방법을 보여줍니다.

IOrderedQueryable<Order> freightQuery =
    from ord in db.Orders
    where ord.ShipCity == "London"
    orderby ord.Freight
    select ord;

foreach (Order ordObj in freightQuery)
{
    Console.WriteLine("Order ID = {0}, Freight = {1}",
        ordObj.OrderID, ordObj.Freight);
}
Dim freightQuery = _
    From ord In db.Orders _
    Where ord.ShipCity = "London" _
    Select ord _
    Order By ord.Freight

For Each ordObj In freightQuery
    Console.WriteLine("Order ID = {0}, Freight = {1}", _
        ordObj.OrderID, ordObj.Freight)
Next

예제 3

다음 예제에서는 단가를 최고에서 최저값으로 정렬 Products 합니다.

IOrderedQueryable<Product> priceQuery =
    from prod in db.Products
    orderby prod.UnitPrice descending
    select prod;

foreach (Product prodObj in priceQuery)
{
    Console.WriteLine("Product ID = {0}, Unit Price = {1}",
        prodObj.ProductID, prodObj.UnitPrice);
}
Dim priceQuery = _
    From prod In db.Products _
    Select prod _
    Order By prod.UnitPrice Descending

For Each prodObj In priceQuery
    Console.WriteLine("Product ID = {0}, Unit Price = {1}", _
       prodObj.ProductID, prodObj.UnitPrice)
Next

예제 4

다음 예제에서는 복합 OrderBy 을 사용하여 도시별로 정렬한 다음 연락처 이름을 기준으로 정렬 Customers 합니다.

IOrderedQueryable<Customer> custQuery =
    from cust in db.Customers
    orderby cust.City, cust.ContactName
    select cust;

foreach (Customer custObj in custQuery)
{
    Console.WriteLine("City = {0}, Name = {1}", custObj.City,
        custObj.ContactName);
}

Dim custQuery = _
    From cust In db.Customers _
    Select cust _
    Order By cust.City, cust.ContactName

For Each custObj In custQuery
    Console.WriteLine("City = {0}, Name = {1}", custObj.City, _
        custObj.ContactName)
Next

예제 5

다음 예에서는 EmployeeID 1의 주문을 ShipCountry에 따라 먼저 정렬하고, 그런 다음 화물을 높은 것에서 낮은 것 순으로 정렬합니다.

IOrderedQueryable<Order> ordQuery =
    from ord in db.Orders
    where ord.EmployeeID == 1
    orderby ord.ShipCountry, ord.Freight descending
    select ord;

foreach (Order ordObj in ordQuery)
{
    Console.WriteLine("Country = {0}, Freight = {1}",
        ordObj.ShipCountry, ordObj.Freight);
}
Dim ordQuery = _
    From ord In db.Orders _
    Where CInt(ord.EmployeeID.Value) = 1 _
    Select ord _
    Order By ord.ShipCountry, ord.Freight Descending

For Each ordObj In ordQuery
    Console.WriteLine("Country = {0}, Freight = {1}", _
        ordObj.ShipCountry, ordObj.Freight)
Next

예제 6

다음 예제에서는 OrderBy, Max, GroupBy 연산자를 결합하여 각 범주에서 단가가 가장 높은 Products을 찾아낸 후, 범주 ID 기준으로 그룹을 정렬합니다.

var highPriceQuery =
    from prod in db.Products
    group prod by prod.CategoryID into grouping
    orderby grouping.Key
    select new
    {
        grouping.Key,
        MostExpensiveProducts =
            from prod2 in grouping
            where prod2.UnitPrice == grouping.Max(p3 => p3.UnitPrice)
            select prod2
    };

foreach (var prodObj in highPriceQuery)
{
    Console.WriteLine(prodObj.Key);
    foreach (var listing in prodObj.MostExpensiveProducts)
    {
        Console.WriteLine(listing.ProductName);
    }
}
Dim highPriceQuery = From prod In db.Products _
                     Group prod By prod.CategoryID Into grouping = Group _
                     Order By CategoryID _
                     Select CategoryID, _
                     MostExpensiveProducts = _
                         From prod2 In grouping _
                         Where prod2.UnitPrice = _
                         grouping.Max(Function(p3) p3.UnitPrice)

For Each prodObj In highPriceQuery
    Console.WriteLine(prodObj.CategoryID)
    For Each listing In prodObj.MostExpensiveProducts
        Console.WriteLine(listing.ProductName)
    Next
Next

Northwind 샘플 데이터베이스에 대해 이전 쿼리를 실행하는 경우 결과는 다음과 유사합니다.

1

Côte de Blaye

2

Vegie-spread

3

Sir Rodney's Marmalade

4

Raclette Courdavault

5

Gnocchi di nonna Alice

6

Thüringer Rostbratwurst

7

Manjimup Dried Apples

8

Carnarvon Tigers

참고하십시오