HOW TO:排序序列中的項目 (LINQ to SQL)
更新: November 2007
使用 OrderBy 運算子,可以根據一個或多個索引鍵來排序序列。
注意事項: |
---|
LINQ to SQL 是設計為支援根據簡單基本型別 (Primitive Type) (如 string、int 等) 的排序。但不支援複雜多重值類別 (如匿名型別) 的排序,也不支援 byte 資料型別。 |
範例
下列範例會根據雇用日期來排序 Employees。
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
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);
}
下列範例使用 where,根據運費來排序運送至 London 的 Orders。
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
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);
}
下列範例會根據單價,從最高到最低來排序 Products。
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
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);
}
下列範例使用複合 OrderBy,先根據城市再根據連絡人名稱來排序 Customers。
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
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);
}
下列範例先根據送貨國家再根據最高到最低的運費,來排序 EmployeeID 1 的訂單。
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
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);
}
下列範例合併 OrderBy、Max 和 GroupBy 運算子,尋找每個分類中單價最高的 Products,然後再根據分類 ID 來排序群組。
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
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);
}
}
如果您對 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