在序列中对元素进行排序

使用 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 按运费对运往 OrdersLondon 进行排序。

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

下面的示例先按 ShipCountry 对来自 EmployeeID 1 的订单进行排序,然后按运费从高到低进行排序。

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

下面的示例结合使用 OrderByMaxGroupBy 运算符查找每个类别中单价最高的 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

请参阅