LINQ使用Orderby、ThenBy实现多字段的排序

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"));
    }
}

执行结果如下图:

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

pan_junbiao

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值