obsidian/笔记文件/2.笔记/LINQ使用Orderby、ThenBy实现多字段的排序.md
2025-03-26 00:02:56 +08:00

3.7 KiB
Raw Permalink Blame History

#unity/日常积累

LINQ中的排序操作符包括OrderBy、OrderByDescending、ThenBy、ThenByDescending、Reverse提供了升序或者降序排序。 OrderBy按升序对序列的元素进行排序。 OrderByDescending按降序对序列的元素排序。 ThenBy按升序对序列中的元素执行后续排序。 ThenByDescending按降序对序列中的元素执行后续排序。 Reverse反转序列中元素的顺序。

示例:将员工列表进行排序,排序要求: 1、按部门编号升序 2、按员工薪资降序 3、按员工入职时间升序

方式一:

empList = empList.OrderBy(a => a.DeptID).ThenByDescending(a => a.Salary).ThenBy(a => a.EntryTime).ToList();

方式二:

empList = (from emp in empList
            orderby emp.DeptID ascending,
            emp.Salary descending,
            emp.EntryTime ascending
            select emp).ToList();

完整代码:

static void Main(string[] args)
{
    //获取员工列表
    List<Employee> empList = GetEmployeeList();
 
    //打印排序前列表
    System.Console.WriteLine("排序前:");
    empList.ForEach(a => System.Console.WriteLine(a.ToString()));
 
    //使用LINQ执行排序
    empList = empList.OrderBy(a => a.DeptID).ThenByDescending(a => a.Salary).ThenBy(a => a.EntryTime).ToList();
 
    //打印排序后列表
    System.Console.WriteLine("");
    System.Console.WriteLine("排序后:");
    empList.ForEach(a => System.Console.WriteLine(a.ToString()));
    System.Console.ReadLine();
}
 
/// <summary>
/// 获取员工列表
/// </summary>
public static List<Employee> GetEmployeeList()
{
    List<Employee> result = new List<Employee>();
    result.Add(new Employee() { Name = "张伟伟", DeptID = 3, DeptName = "市场部", Salary = 1500, EntryTime = DateTime.Parse("2016-05-12")});
    result.Add(new Employee() { Name = "李涛涛", DeptID = 2, DeptName = "财务部", Salary = 1600, EntryTime = DateTime.Parse("2017-02-16") });
    result.Add(new Employee() { Name = "王亮亮", DeptID = 1, DeptName = "研发部", Salary = 1900, EntryTime = DateTime.Parse("2018-10-25") });
    result.Add(new Employee() { Name = "孙红红", DeptID = 1, DeptName = "研发部", Salary = 1900, EntryTime = DateTime.Parse("2018-08-03") });
    result.Add(new Employee() { Name = "黄苗苗", DeptID = 3, DeptName = "市场部", Salary = 2200, EntryTime = DateTime.Parse("2016-09-06") });
    result.Add(new Employee() { Name = "蔡明明", DeptID = 1, DeptName = "研发部", Salary = 3500, EntryTime = DateTime.Parse("2012-11-25") });
    result.Add(new Employee() { Name = "吴慧慧", DeptID = 2, DeptName = "财务部", Salary = 1800, EntryTime = DateTime.Parse("2018-07-26") });
    result.Add(new Employee() { Name = "杨梅梅", DeptID = 3, DeptName = "市场部", Salary = 2200, EntryTime = DateTime.Parse("2017-02-15") });
    return result;
}


创建员工信息类Emplayee.cs

/// <summary>
/// 员工信息类
/// </summary>
public class Employee
{
    /// <summary>
    /// 员工名称
    /// </summary>
    public string Name { get; set; }
 
    /// <summary>
    /// 部门编号
    /// </summary>
    public int DeptID { get; set; }
 
    /// <summary>
    /// 部门名称
    /// </summary>
    public string DeptName { get; set; }
 
    /// <summary>
    /// 薪资
    /// </summary>
    public decimal Salary { get; set; }
 
    /// <summary>
    /// 入职时间
    /// </summary>
    public DateTime EntryTime { get; set; }
 
    public override string ToString()
    {
        return String.Format("{0}{1};薪资:{2}元;入职时间:{3}", this.Name, this.DeptName, this.Salary, this.EntryTime.ToString("yyyy-MM-dd"));
    }
}

执行结果如下图:

!Pasted image 20240122142802.png