C#排序 轉

本文連接:https://blog.csdn.net/fysuccess/article/details/36416255
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>();
 ———————————————— 
版權聲明:本文爲CSDN博主「fysuccess」的原創文章,遵循CC 4.0 by-sa版權協議,轉載請附上原文出處連接及本聲明。
原文連接:https://blog.csdn.net/fysuccess/article/details/36416255
相關文章
相關標籤/搜索