Sorting Operator Description
OrderBy 通过给定的字段进行升序 降序 排序
OrderByDescending 通过给定字段进行降序排序,仅在方法查询中使用
ThenBy 第二级升序排序,仅在方法查询中使用
ThenByDescending 第二级降序排序,仅在方法查询中使用
Reverse 反转集合,仅在方法查询中使用
IList<Student> studentList = new List<Student>() {
new Student() { StudentID = , StudentName = "John", Age = } ,
new Student() { StudentID = , StudentName = "Steve", Age = } ,
new Student() { StudentID = , StudentName = "Bill", Age = } ,
new Student() { StudentID = , StudentName = "Ram" , Age = } ,
new Student() { StudentID = , StudentName = "Ron" , Age = }
}; 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 = , StudentName = "John", Age = } ,
new Student() { StudentID = , StudentName = "Steve", Age = } ,
new Student() { StudentID = , StudentName = "Bill", Age = } ,
new Student() { StudentID = , StudentName = "Ram" , Age = } ,
new Student() { StudentID = , StudentName = "Ron" , Age = }
}; var studentsInAscOrder = studentList.OrderBy(s => s.StudentName);

OrderByDescending

IList<Student> studentList = new List<Student>() {
new Student() { StudentID = , StudentName = "John", Age = } ,
new Student() { StudentID = , StudentName = "Steve", Age = } ,
new Student() { StudentID = , StudentName = "Bill", Age = } ,
new Student() { StudentID = , StudentName = "Ram" , Age = } ,
new Student() { StudentID = , StudentName = "Ron" , Age = }
}; var studentsInDescOrder = studentList.OrderByDescending(s => s.StudentName);

多个排序

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

注意:

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

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

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

LINQ 学习路程 -- 查询操作 OrderBy & OrderByDescending的更多相关文章

  1. LINQ 学习路程 -- 查询操作 Expression Tree

    表达式树就像是树形的数据结构,表达式树中的每一个节点都是表达式, 表达式树可以表示一个数学公式如:x<y.x.<.y都是一个表达式,并构成树形的数据结构 表达式树使lambda表达式的结构 ...

  2. LINQ 学习路程 -- 查询操作 Deferred Execution of LINQ Query 延迟执行

    延迟执行是指一个表达式的值延迟获取,知道它的值真正用到. 当你用foreach循环时,表达式才真正的执行. 延迟执行有个最重要的好处:它总是给你最新的数据 实现延迟运行 你可以使用yield关键字实现 ...

  3. LINQ 学习路程 -- 查询操作 Join

    Join操作是将两个集合联合 Joining Operators Usage Join 将两个序列连接并返回结果集 GroupJoin 根据key将两个序列连接返回,像是SQL中的Left Join ...

  4. LINQ 学习路程 -- 查询操作 where

    1.where Filtering Operators Description Where Returns values from the collection based on a predicat ...

  5. LINQ 学习路程 -- 查询操作 GroupBy ToLookUp

    Grouping Operators Description GroupBy GroupBy操作返回根据一些键值进行分组,每组代表IGrouping<TKey,TElement>对象 To ...

  6. LINQ 学习路程 -- 查询操作 let into关键字

    IList<Student> studentList = new List<Student>() { , StudentName = } , , StudentName = } ...

  7. LINQ 学习路程 -- 查询操作 Aggregate

    聚合操作执行数学的运算,如平均数.合计.总数.最大值.最小值 Method Description Aggregate 在集合上执行自定义聚集操作 Average 求平均数 Count 求集合的总数 ...

  8. LINQ 学习路程 -- 查询操作 Select, SelectMany

    IList<Student> studentList = new List<Student>() { , StudentName = "John" }, , ...

  9. LINQ 学习路程 -- 查询操作 ThenBy & ThenByDescending

    IList<Student> studentList = new List<Student>() { , StudentName = } , , StudentName = } ...

随机推荐

  1. SpringBoot启动流程分析(五):SpringBoot自动装配原理实现

    SpringBoot系列文章简介 SpringBoot源码阅读辅助篇: Spring IoC容器与应用上下文的设计与实现 SpringBoot启动流程源码分析: SpringBoot启动流程分析(一) ...

  2. 跟我一起写 Makefile(三)[转]

    原文链接 http://bbs.chinaunix.net/thread-408225-1-1.html(出处: http://bbs.chinaunix.net/) make 的运行—————— 一 ...

  3. 在jfinal的Controller中接受json数据

    JFinal中接收URL中的参数或者model中的参数是很方便的,但是对于web2.0的网站来说,经常会以json方式提交比较复杂的数据,比如一个查询,包含了各种过滤条件和排序分页,前端脚本可能提交的 ...

  4. iOS Masonry 查看更多 收起

    Masonry 查看更多 收起效果实现,带动画 demo下载地址: https://github.com/qqcc1388/MasonryDemo

  5. mapreduce 查找共同好友

    A:B,C,D,F,E,O B:A,C,E,K C:F,A,D,I D:A,E,F,L E:B,C,D,M,L F:A,B,C,D,E,O,M G:A,C,D,E,F H:A,C,D,E,O I:A, ...

  6. 【数据挖掘】分类之kNN(转载)

    [数据挖掘]分类之kNN 1.算法简介 kNN的思想很简单:计算待分类的数据点与训练集所有样本点,取距离最近的k个样本:统计这k个样本的类别数量:根据多数表决方案,取数量最多的那一类作为待测样本的类别 ...

  7. linux无线网络配置_转

    转自:http://www.cnblogs.com/dartagnan/archive/2010/12/05/2003521.html   一位资生linux 原文:http://www.hpl.hp ...

  8. Google Code Jam 2014 Round 1 A:Problem A Charging Chaos

    Problem Shota the farmer has a problem. He has just moved into his newly built farmhouse, but it tur ...

  9. 【JMeter4.0学习(十一)】之JMeter对(Mysql、Oracle)数据库性能测试脚本开发

    一.MySQL数据库链接: 注:下面所产生的问题一律参考详见:<[JMeter4.0]之遇到的问题总结(持续更新)>(包括Mysql.Orcale) 准备:引包,包路径一定要放对位置,参考 ...

  10. Linux 安装json神器 jq

    wget -O jq https://github.com/stedolan/jq/releases/download/jq-1.6/jq-linux64 chmod +x ./jq cp jq /u ...