C#.net工作筆記001---Linq對象查詢在工作中的使用_隨時更新

技術交流QQ羣【JAVA,.NET,BigData,AI】:170933152  

1.對list中的某兩個字段按照升序排序

 testlist=testlist.OrderBy(s=>new{s.cd1,s.cd2}).ToList<TestDto>();

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>();

//先排序再分組

testlist =(List<TestDto>)(from m in testlist orderby m.orderno group m by new {m.nroderno , m.ordernodtl} into mygroup select mygroup);

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章