C# Linq ThenBy 方法
csharpprogrammingserver side programming更新于 2025/9/14 2:21:00
使用 ThenBy() 方法按序列对元素进行排序。
我们有以下字符串数组。
string[] str = { "AAA", "AAAA", "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

