zoukankan      html  css  js  c++  java
  • C# 中使用Linq和Lambda表达式对List<T>进行排序

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

  • 相关阅读:
    OpenSSH服务——密钥登录
    进程管理
    磁盘管理
    文件系统
    shell命令手册
    第一次常用命令手册
    远程连接mobaxterm安装使用
    Linux 系统CentOS 7 64 位安装
    PythonI/O进阶学习笔记_11.python的多进程
    PythonI/O进阶学习笔记_10.python的多线程
  • 原文地址:https://www.cnblogs.com/jett010/p/8963631.html
Copyright © 2011-2022 走看看