C# 多级排序
处理了带多个属性的对象后,就要考虑另一种情形了:按一个字段给查询结果排序是不够的,需要查询顾客,并按照区域使结果以字母顺序排列,再按区域中的国家或城市名称以字母顺序排序。使用LINQ,可方便地完成这个任务,如下面的示例所示。
试—试多级扫后序:BeginningCSharp7_22_9_MultiLevelOrdering\Program.cs
按照下面的步骤在Visual Studio 2017中创建示例:
(1)修改前面的示例 BeginningCSharp7_22_8_SelectDistinclQueryr 或在 C:\BeginnmgGSharp7\Chapter22目录中创建一个新的控制台应用程序 BegirmingCSharp7_22_9_MultiLevelOrdering。
(2)如 BeginningCSharp7_22_8_SelectDistinctQuery 示例所示,创建 Customer 类并初始化 customers 列表 (List<Customer> customers),这些代码与前面示例中的代码完全相同。
(3)在Main()方法的customers列表初始化之后,输入如下所示的查询:
var queryResults =
from c in customers
orderby. c.Region, c.Country, c.City
select new { c.ID, c.Region, c.Country, c,City }
;
(4)结果处理循环和Main()方法的其余代码与前面例子中的相同。
(5)编译并执行程序,从所有顾客中选择出来的属性将先按区域排序,再按国家排序,最后按城市排序,如下所示:
{ ID = 0, Region = Africa, Country = Egypt, City = Cairo }
{ ID = J, Region = Africa, Country = Nigeria, City = Lagos }
{ ID = R, Region = Asia, Country = China, City = Beijing }
{ ID = I, Region = Asia, Country = China, City = Shanghai }
{ ID = D, Region = Asia, Country = India, City = Delhi }
{ ID = B, Region = Asia, Country = India, City = Mumbai }
{ ID = L, Region = Asia, Country = Indonesia, City = Jakarta }
{ ID = P, Region = Asia, Country = Iran, City = Tehran }
{ ID = Mr Region = Asia, Country = Japan, City = Tokyo }
{ ID = G, Region = Asia, Country = Korea, City = Seoul }
{ ID = C, Region = Asia, Country = Pakistan, City = Karachi }
{ ID = H, Region = Asia# Country = Turkey, City = Istanbul }
{ ID = F, Region = Europe, Country = Russia, City = Moscow }
{ ID = Q, Region = Europe, Country = UK^ City = London }
{ ID = K, Region = North America, Country = Mexico, City = Mexico City }
{ ID = N, Region = North America, Country = USA, City = Los Angeles }
{ ID = A, Region = North America, Country = USA, City = New York }
{ ID = E, Region = South America, Country = Brazil, City = Sao Paulo }
{ ID = S, Region = South America, Country = Coloinbiar City = Bogota }
{ ID = T, Region = South America, Country = Peru, City = Lima }
Program finished, press Enter/Return to continue:
示例说明
Customer类和customers列表的初始化与前面例子中的相同。因为要查看所有的顾客,这个査询中没有where子句,但按顺序列出了要排序的字段,它们放在orderby子句的一个用逗号分开的列表中:
orderby c.Region, c,Country, c.City
这很容易,但不太直观,这个简单的字段列表允许放在orderby子句中,但不能放在select子句中,不过这就是LINQ的工作方式。如果知道select子句会创建一个新对象,而根据定义,ordeAy子句会逐字段执行,这样就不会觉得这个字段列表难以理解了。
可给列出的任意字段添加descending关键字,反转该字段的排序顺序。例如,要对查询结果按区域升序排序,再按国家降序排序,只需要在列表中的Coimtry后面加上descending关键字即可,如下所示:
orderby c.Region, c.Country descending/ c.City
添加了descending关键字后,结果如下:
{ ID = J, Region = Africa, Country = Nigeria, City = Lagos }
{ ID = 0, Region = Africa, Country = Egypt, City = Cairo }
{ ID = H, Region = Asia, Country = Turkey, City = Istanbul }
{ ID = C, Region = Asia, Country = Pakistan, City = Karachi }
{ ID = G, Region = Asia, Country = Korea, City = Seoul }
{ ID = M, Region = Asia, Country = Japan, City = Tokyo }
{ ID = P, Region = Asia, Country = Iran, City = Tehran }
{ ID = L, Region = Asia, Country = Indonesia, City = Jakarta }
{ ID = D, Region = Asia, Country = India, City = Delhi }
{ ID = B, Region = Asia, Country = India, City = Mumbai }
{ ID = R, Region = Asia, Country = China, City = Beijing }
{ ID = I, Region = Asia, Country = China, City = Shanghai }
{ ID = Q, Region = Europe, Country = UK, City = London }
{ ID = F, Region = Europe, Country = Russia, City = Moscow }
{ ID = N, Region = North America, Country = USA, City = Los Angeles }
{ ID = A, Region = North America, Country = USA, City = New York }
{ ID = K, Region = North America, Country = Mexico, City = Mexico City }
{ ID = T, Region = South America, Country = Peru, City = Lima }
{ ID = S, Region = South America, Country = Colombiaf City = Bogota }
{ ID = E, Region = South America, Country = Brazil, City = Sao Paulo }
Program finished, press Enter/Return to continue:
注意,即使国家的顺序被反转了,印度和中国的城市仍按升序排序。
点击加载更多评论>>