C# Linq ThenBy 方法
使用 ThenBy() 方法對序列中的元素進行排序。
我們有以下字串陣列。
string[] str = { "AAA", "AAAA", "A", "AAAAA", "AAAAAAAAA" };
現在,使用 Lambda 表示式並在 ThenBy() 方法內部設定一個條件,根據字串的字元數對字串進行排序。
IEnumerable<string> res = str.AsQueryable() .OrderBy(alp => alp.Length).ThenBy(alp => alp);
示例
using System; using System.Linq; using System.Collections.Generic; public class Demo { public static void Main() { string[] str = { "AAA", "AAAA", "A", "AAAAA", "AAAAAAAAA" }; IEnumerable<string> res = str.AsQueryable() .OrderBy(alp => alp.Length).ThenBy(alp => alp); foreach (string arr in res) Console.WriteLine(arr); } }
輸出
A AAA AAAA AAAAA AAAAAAAAA
推廣內容