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>();
多字段: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>();
本文介绍C#中List<T>的排序方法,包括使用LINQ表达式按单字段或多字段进行升序或降序排列,以及如何通过Lambda表达式实现复杂的排序需求。

1438

被折叠的 条评论
为什么被折叠?



