zoukankan      html  css  js  c++  java
  • LINQ 学习路程 -- 查询操作 OrderBy & OrderByDescending

    Sorting OperatorDescription
    OrderBy 通过给定的字段进行升序 降序 排序
    OrderByDescending 通过给定字段进行降序排序,仅在方法查询中使用
    ThenBy 第二级升序排序,仅在方法查询中使用
    ThenByDescending 第二级降序排序,仅在方法查询中使用
    Reverse 反转集合,仅在方法查询中使用
    IList<Student> studentList = new List<Student>() { 
        new Student() { StudentID = 1, StudentName = "John", Age = 18 } ,
        new Student() { StudentID = 2, StudentName = "Steve",  Age = 15 } ,
        new Student() { StudentID = 3, StudentName = "Bill",  Age = 25 } ,
        new Student() { StudentID = 4, StudentName = "Ram" , Age = 20 } ,
        new Student() { StudentID = 5, StudentName = "Ron" , Age = 19 } 
    };
    
    var orderByResult = from s in studentList
                       orderby s.StudentName 
                       select s;
    
    var orderByDescendingResult = from s in studentList
                       orderby s.StudentName descending
                       select s;

    OrderBy扩展方法有两个重载方法,第一个方法接受一个类型参数,你可以指定通过哪个字段进行排序

    第二个方法接受一个实现IComparer的类型,用户可以自定义排序

    public static IOrderedEnumerable<TSource> OrderBy<TSource, TKey>(this IEnumerable<TSource> source, 
                                                                     Func<TSource, TKey> keySelector);
    
    public static IOrderedEnumerable<TSource> OrderBy<TSource, TKey>(this IEnumerable<TSource> source, 
                                                                     Func<TSource, TKey> keySelector, 
                                                                     IComparer<TKey> comparer);
    IList<Student> studentList = new List<Student>() { 
        new Student() { StudentID = 1, StudentName = "John", Age = 18 } ,
        new Student() { StudentID = 2, StudentName = "Steve",  Age = 15 } ,
        new Student() { StudentID = 3, StudentName = "Bill",  Age = 25 } ,
        new Student() { StudentID = 4, StudentName = "Ram" , Age = 20 } ,
        new Student() { StudentID = 5, StudentName = "Ron" , Age = 19 } 
    };
    
    var studentsInAscOrder = studentList.OrderBy(s => s.StudentName);

    OrderByDescending

    IList<Student> studentList = new List<Student>() { 
        new Student() { StudentID = 1, StudentName = "John", Age = 18 } ,
        new Student() { StudentID = 2, StudentName = "Steve",  Age = 15 } ,
        new Student() { StudentID = 3, StudentName = "Bill",  Age = 25 } ,
        new Student() { StudentID = 4, StudentName = "Ram" , Age = 20 } ,
        new Student() { StudentID = 5, StudentName = "Ron" , Age = 19 } 
    };
    
    var studentsInDescOrder = studentList.OrderByDescending(s => s.StudentName);

    多个排序

    可以使用多个字段以逗号隔开进行排序,集合首先以第一个字段进行排序,如果第一个字段有相同的值,则根据第二个字段进行排序

    注意:

    LINQ包含五种排序操作:OrderBy、OrderByDescending、ThenBy、ThenByDescending、Reverse

    查询语言不支持OrderByDescending、ThenBy、ThenByDescending、Reverse,它仅支持Order By从句后面跟ascending、descending

    查询语法支持多字段排序,

  • 相关阅读:
    04-老马jQuery教程-DOM节点操作及位置和大小
    03-老马jQuery教程-DOM操作
    02-老马jQuery教程-jQuery事件处理
    01-老马jQuery教程-jQuery入口函数及选择器
    08Vue.js快速入门-Vue综合实战项目
    09Vue.js快速入门-Vue入门之Vuex实战
    07Vue.js快速入门-Vue路由详解
    06Vue.js快速入门-Vue组件化开发
    整套高质量前端基础到高级视频教程免费发布
    05-Vue入门系列之Vue实例详解与生命周期
  • 原文地址:https://www.cnblogs.com/lanpingwang/p/6602258.html
Copyright © 2011-2022 走看看