位置:首頁 > 軟件操作教程 > 編程開發(fā) > C# > 問題詳情

C# 多級排序

提問人:劉團圓發(fā)布時間:2020-12-11

    處理了帶多個屬性的對象后,就要考慮另一種情形了:按一個字段給查詢結果排序是不夠的,需要查詢顧客,并按照區(qū)域使結果以字母順序排列,再按區(qū)域中的國家或城市名稱以字母順序排序。使用LINQ,可方便地完成這個任務,如下面的示例所示。


試—試多級掃后序:BeginningCSharp7_22_9_MultiLevelOrdering\Program.cs

按照下面的步驟在Visual Studio 2017中創(chuàng)建示例:

(1)修改前面的示例 BeginningCSharp7_22_8_SelectDistinclQueryr 或在 C:\BeginnmgGSharp7\Chapter22目錄中創(chuàng)建一個新的控制臺應用程序 BegirmingCSharp7_22_9_MultiLevelOrdering。

(2)如 BeginningCSharp7_22_8_SelectDistinctQuery 示例所示,創(chuàng)建 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)結果處理循環(huán)和Main()方法的其余代碼與前面例子中的相同。

(5)編譯并執(zhí)行程序,從所有顧客中選擇出來的屬性將先按區(qū)域排序,再按國家排序,最后按城市排序,如下所示:

{ 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子句會創(chuàng)建一個新對象,而根據(jù)定義,ordeAy子句會逐字段執(zhí)行,這樣就不會覺得這個字段列表難以理解了。

    可給列出的任意字段添加descending關鍵字,反轉該字段的排序順序。例如,要對查詢結果按區(qū)域升序排序,再按國家降序排序,只需要在列表中的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:

注意,即使國家的順序被反轉了,印度和中國的城市仍按升序排序。

繼續(xù)查找其他問題的答案?

相關視頻回答
回復(0)
返回頂部