博客
关于我
C#中使用OrderBy和ThenBy等方法对List集合进行排序
阅读量:507 次
发布时间:2019-03-07

本文共 586 字,大约阅读时间需要 1 分钟。

在C#的List集合中,对于Order对象的List集合进行排序时,可以使用OrderBy和ThenBy系列方法来指定排序规则。OrderBy用于按指定属性升序排序,而OrderByDescending用于降序排序。而ThenBy和ThenByDescending方法则用于在已有的排序基础上,按第二个属性进行排序。

Order类的定义如下:

public class Order{    public string DepCode { get; set; }    public string DepName { get; set; }    public decimal Amount { get; set; }}

当需要按科室编码DepCode升序,然后再按订单金额Amount降序排序时,可以使用以下代码:

var orderList = orderList.OrderBy(t => t.DepCode).ThenByDescending(t => t.Amount).ToList();

这个代码首先使用OrderBy按DepCode升序排序,然后按照Amount降序排序。t代表list中的Order对象,从而实现多层级的排序。在这种情况下,如果DepCode相同,Amount将按照降序排列,从高到低。这种方法既简洁又高效,能够有效地满足多条件排序的需求。

转载地址:http://tscjz.baihongyu.com/

你可能感兴趣的文章
Python WebDriver如何打印整个页面源(html)
查看>>
Python WebSocket自动化测试:构建高效接口测试框架
查看>>
Python Web开发
查看>>
Redis 配置文件杂项。
查看>>
Python web自动化测试 —— 文件上传
查看>>
Python web自动化测试 —— 文件上传!
查看>>
Python Web自动化测试开发环境搭建(附安装包与虚拟机环境)
查看>>
python win32api键盘_Python win32api.keybd_event模拟键盘输入
查看>>
python Windows显示当前鼠标坐标
查看>>
python Workbook 表格宽度
查看>>
python write报错a byte-like object is required.not str
查看>>
PYTHON调用大模型实现图片生成
查看>>
python yaml.dump 缩进错误
查看>>
Python yield generator
查看>>
Python yield 使用浅析
查看>>
Python yield解析:深入理解生成器的魔力
查看>>
Python You are using pip version 10.0.1, however version 19.3.1 is available.
查看>>
python zipfile模块学习笔记(一)
查看>>
Python zip函数 详解(全)
查看>>
Python \r\n与\n的转换
查看>>