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. java之数字彩虹雨

    © 版权声明:本文为博主原创文章,转载请注明出处 数字彩虹雨: 从上至下,随机出现一串字符串,以不同的速度运行到底部:类似于黑客帝国里面的场景 GitHub:https://github.com/Ta ...

  2. C# 接口中的索引器

    索引器可在 接口(C# 参考) 上声明.接口索引器的访问器与类索引器的访问器具有以下方面的不同: 接口访问器不使用修饰符. 接口访问器没有体. 因此,访问器的用途是指示索引器是读写.只读还是只写.以下 ...

  3. executable null\bin\winutils.exe in the Hadoop binaries.

    在windows 使用eclipse远程调用hadoop集群时抛出下面异常 executable null\bin\winutils.exe in the Hadoop binaries. 这个问题 ...

  4. [译]NeHe教程 - 你的第一个多边形

    原文: Your First Polygon 在第一节中我讲解了如何创建OpenGL窗体.本节我会讲解如何创建三角形和四边形.我们会用GL_TRIANGLES来创建三角形,用GL_GUADS创建四边形 ...

  5. route 命令

    Linux系统的route命令用于显示和操作IP路由表(show / manipulate the IP routing table).要实现两个不同的子网之间的通信,需要一台连接两个网络的路由器,或 ...

  6. hadoop System times on machines may be out of sync. Check system time and time zones.

    之前环境一直好好的,由于玩坏了一个mini3只能复制一个了,但是复制之后就出现这个问题了 解决办法是 设置xshell向每一个窗口发消息http://mofansheng.blog.51cto.com ...

  7. Java以指定格式输入数字

    package com.ylx; import java.text.DecimalFormat; public class Test { public static void main(String[ ...

  8. Weblogic配置故障转移

    前提:实现负载均衡,即当访问项目时,会通过代理服务器将请求分发到不同的服务器上. weblogic的故障转移配置 在项目的WEB-INF目录下创建weblogic.xml <!DOCTYPE w ...

  9. Web存储使用详解(本地存储、会话存储)

    Web存储使用详解(本地存储.会话存储)1,Web存储介绍HTML5的Web存储功能是让网页在用户计算机上保存一些信息.Web存储又分为两种:(1)本地存储,对应 localStorage 对象.用于 ...

  10. 第4章 URL管理器和实现方法

    URL管理器:管理待抓取URL集合和已抓取URL集合 -- 防止重复抓取.防止循环抓取 URL需要支持哪些功能: 添加新URL到待爬取集合中.判断待添加URL是否在容器中,判断是否还有待爬取URL,获 ...