当前位置: 首页 >服务端 > C# 中使用Linq和Lambda表达式对List 进行排序

C# 中使用Linq和Lambda表达式对List 进行排序

C#中List<T>排序的两种方法

 

List<Student> stu = (List<Student>)Session["StudentList"];

 

Linq表达式:
//按学号降序
List<Student> stuList = (from s instu orderby s.stuNOdescending select s).ToList<Student>();

//按学号升序
List<Student> stuList = (from s instu orderby s.stuNO  select s).ToList<Student>();


使用Lambda表达式排序: //按学号降序
单字段:List<Student> stuList= stu.OrderByDescending(s=> s.orderid).ToList<Student>();
多字段:List<Student> stuList= stu.OrderByDescending(s=> new{s.stuNO,s.stuName}).ToList<Student>();

//按学号升序
单字段:List<Student> stuList= stu.OrderBy(s=> s.stuNO).ToList<Student>();
多字段:List<Student> stuList= stu.OrderBy(s=> new{s.stuNO,s.stuName}).ToList<Student>();


多字段主次顺序排序情况,先按no排序,再按name排序
List<Student> stuList= stu.OrderBy(s=> s.stuNO).ThenBy(s=> s.stuName).ToList<Student>();

List<Student> stuList= stu.OrderBy(s=> new{ s.stuNO }).ThenBy(s=> new{s.stuName}).ToList<Student>();

作者:jett010
来源链接:https://www.cnblogs.com/jett010/p/8963631.html

版权声明:
1、JavaClub(https://www.javaclub.cn)以学习交流为目的,由作者投稿、网友推荐和小编整理收藏优秀的IT技术及相关内容,包括但不限于文字、图片、音频、视频、软件、程序等,其均来自互联网,本站不享有版权,版权归原作者所有。

2、本站提供的内容仅用于个人学习、研究或欣赏,以及其他非商业性或非盈利性用途,但同时应遵守著作权法及其他相关法律的规定,不得侵犯相关权利人及本网站的合法权利。
3、本网站内容原作者如不愿意在本网站刊登内容,请及时通知本站(javaclubcn@163.com),我们将第一时间核实后及时予以删除。





本文链接:https://www.javaclub.cn/server/117220.html

标签:Linux
分享给朋友:

“C# 中使用Linq和Lambda表达式对List 进行排序” 的相关文章

实习第一周(Golang) 2022年05月14日 16:18:05
linux yum命令详解 2022年05月19日 19:54:29
引用类型 2022年05月21日 11:41:30
为了应付优性测评而自欺欺人 2022年06月02日 22:00:25
概率论快速学习01:计数 2022年06月03日 22:33:14
深入浅出: 大小端模式 2022年06月03日 23:10:37
Windows下python环境变量配置 2022年06月07日 06:54:13