LINQ 学习路程 -- 查询操作 OrderBy & OrderByDescending
| 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的更多相关文章
- LINQ 学习路程 -- 查询操作 Expression Tree
表达式树就像是树形的数据结构,表达式树中的每一个节点都是表达式, 表达式树可以表示一个数学公式如:x<y.x.<.y都是一个表达式,并构成树形的数据结构 表达式树使lambda表达式的结构 ...
- LINQ 学习路程 -- 查询操作 Deferred Execution of LINQ Query 延迟执行
延迟执行是指一个表达式的值延迟获取,知道它的值真正用到. 当你用foreach循环时,表达式才真正的执行. 延迟执行有个最重要的好处:它总是给你最新的数据 实现延迟运行 你可以使用yield关键字实现 ...
- LINQ 学习路程 -- 查询操作 Join
Join操作是将两个集合联合 Joining Operators Usage Join 将两个序列连接并返回结果集 GroupJoin 根据key将两个序列连接返回,像是SQL中的Left Join ...
- LINQ 学习路程 -- 查询操作 where
1.where Filtering Operators Description Where Returns values from the collection based on a predicat ...
- LINQ 学习路程 -- 查询操作 GroupBy ToLookUp
Grouping Operators Description GroupBy GroupBy操作返回根据一些键值进行分组,每组代表IGrouping<TKey,TElement>对象 To ...
- LINQ 学习路程 -- 查询操作 let into关键字
IList<Student> studentList = new List<Student>() { , StudentName = } , , StudentName = } ...
- LINQ 学习路程 -- 查询操作 Aggregate
聚合操作执行数学的运算,如平均数.合计.总数.最大值.最小值 Method Description Aggregate 在集合上执行自定义聚集操作 Average 求平均数 Count 求集合的总数 ...
- LINQ 学习路程 -- 查询操作 Select, SelectMany
IList<Student> studentList = new List<Student>() { , StudentName = "John" }, , ...
- LINQ 学习路程 -- 查询操作 ThenBy & ThenByDescending
IList<Student> studentList = new List<Student>() { , StudentName = } , , StudentName = } ...
随机推荐
- atitit.bsh BeanShell 的动态脚本使用java
atitit.bsh BeanShell 的动态脚本使用java 1.1. BeanShell是一个小巧免费的JAVA源码解释器 ,支持对象式的脚本语言特性,亦可嵌入到JAVA源代码中. 亦可嵌入到J ...
- C++中没有定义类的引用。
在有时候由于类太大.须要在类在后面定义: 比如: class Y{ void f(X); }; class X{ //一些成员数据和函数 }; //error 由于c++要求不论什么一个变量在引用之前 ...
- const_cast去除const限制,同一片内存
本质很简单,但一些优化 和 编程上的错误,却让人看不清本质. :const_cast<type_id> (expression) 该运算符用来修改类型的const或volatile属性.除 ...
- oracle索引的理解
1.当查询表时where条件中有多个索引时,优先使用主键索引,其它索引会失效. 2.当查询的返回的数据占总量数据的百分比小于20%时,建索引才有效果 3.不是主键的索引值可以为空,主键索引不能为空. ...
- windows upd广播包无法发送到局域网解决方法
不能发送广播包的电脑和可以发送广播报的主机对比,发现不能发送广播报的主机上都有安装虚拟机,也有虚拟网卡,将所有的虚拟网卡关闭,然后再进行测试,都正常了,无论是Win7,Win10还是Xp. 禁用VMw ...
- 【Python+selenium】之奇怪问题总结
问题1: <_io.TextIOWrapper name='<stderr>' mode='w' encoding='UTF-8'> Time Elapsed: 0:00:04 ...
- 【转】【FTP】之windows8.1上搭建FTP服务器方法
参考地址:<windows8.1上搭建FTP服务器方法>
- UISegmentedControl 功能简单 分析
UISegmentedControl类似于UIButton,它可以提供多个选择操作,响应事件,但具有很大的局限性,我们更多的是使用自定义的,不过在这里还是介绍下它的基本用法. NSArray *seg ...
- 解决Command "laravoole" is not defined.
版权声明:本文为博主原创文章,未经博主允许不得转载. GitHub地址:https://github.com/garveen/laravoole 先来执行正常的安装流程: 安装 要开始,将larav ...
- 将PHP 5.3.3 (cli)升级到PHP 5.6.31 (cli)
centos默认系统安装的是php5.3 [root@sz-local1 scripts]# rpm -qa |grep phpphp-pdo-5.3.3-47.el6.x86_64php-mysql ...