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. jquery字符串转json

    var data; var json='[{"CityId":18,"CityName":"西安","ProvinceId&quo ...

  2. 二分Kmeans的java实现

    刚刚研究了Kmeans.Kmeans是一种十分简单的聚类算法.可是他十分依赖于用户最初给定的k值.它无法发现随意形状和大小的簇.最适合于发现球状簇.他的时间复杂度为O(tkn).kmeans算法有两个 ...

  3. bzoj 2458: [BeiJing2011]最小三角形 题解

    [前言]话说好久没有写题解了.到暑假了反而忙.o(╯□╰)o [原题] 2458: [BeiJing2011]最小三角形 Time Limit: 10 Sec  Memory Limit: 128 M ...

  4. rbg大神的主页

    http://www.rossgirshick.info/ Ross Girshick (rbg)Research ScientistFacebook AI Research (FAIR) r...@ ...

  5. sklearn函数白板

    #使用make_classification构造500个样本,每个样本有20个feature from sklearn.datasets import make_classification X, y ...

  6. jquery将form表单序列化常json

    var formData = {};$.each(form.serializeArray(),function(i, item){ formData[item.name] = item.value;} ...

  7. inline用于替代宏函数

    在C&C++中 一.inline关键字用来定义一个类的内联函数,引入它的主要原因是用它替代C中表达式形式的宏定义. 表达式形式的宏定义一例: #define ExpressionName(Va ...

  8. Highways - poj 2485 (Prim 算法)

      Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 24383   Accepted: 11243 Description T ...

  9. ActiveMQ与xml rpc

    最近项目在做平台间的消息传递,也让我对平台间消息的传递进行了深一步的探讨.先叙述一下概况 公司上一个版本用的是winform做的监控软件,主要做设备的通信和控制,基本的连接如下

  10. erlang的非平衡的二叉树的操作

    -module(tree1). -export([test1/0]). lookup(Key,nil) -> not_found; lookup(Key,{Key,Value,_,_}) -&g ...