LINQ排序操作符包括:OrderBy、OrderByDescending、ThenBy、ThenByDescending及Reverse。

1. OrderBy

1>. 原型定义

public static IOrderedQueryable<TSource> OrderBy<TSource, TKey>(this IQueryable<TSource> source, Expression<Func<TSource, TKey>> keySelector);
public static IOrderedQueryable<TSource> OrderBy<TSource, TKey>(this IQueryable<TSource> source, Expression<Func<TSource, TKey>> keySelector, IComparer<TKey> comparer);

2>. 示例

var products = from p in context.Products
orderby p.ProductID
select p;
IEnumerable<Product> products = context.Products.OrderBy(p => p.ProductID);

  当扩展方法中有多个OrderBy操作符出现时,LINQ不会提示错误,将会以最后出现的OrderBy属性进行排序。

var products = context.Products
.OrderBy(p => p.ProductID)
.OrderBy(p => p.ProductName);

  上面的例子中将按照ProductName进行升序排序。

2. OrderByDescending

1>. 原型定义

public static IOrderedQueryable<TSource> OrderByDescending<TSource, TKey>(this IQueryable<TSource> source, Expression<Func<TSource, TKey>> keySelector);
public static IOrderedQueryable<TSource> OrderByDescending<TSource, TKey>(this IQueryable<TSource> source, Expression<Func<TSource, TKey>> keySelector, IComparer<TKey> comparer);

2>. 示例

var products = from p in context.Products
orderby p.ProductID descending
select p;
var products = context.Products.OrderByDescending(p => p.ProductID);

3. ThenBy

  在使用ThenBy操作符之前,扩展方法表达式中必须有OrderBy或OrderByDescending。ThenBy可以多次出现,排序字段累加。

1>. 原型定义

public static IOrderedQueryable<TSource> ThenBy<TSource, TKey>(this IOrderedQueryable<TSource> source, Expression<Func<TSource, TKey>> keySelector);
public static IOrderedQueryable<TSource> ThenBy<TSource, TKey>(this IOrderedQueryable<TSource> source, Expression<Func<TSource, TKey>> keySelector, IComparer<TKey> comparer);

2>. 示例

var products = from p in context.Products
orderby p.ProductID, p.ProductName
select p;
var products = context.Products
.OrderBy(p => p.ProductID)
.ThenBy(p => p.ProductName);
var products = context.Products
.OrderBy(p => p.ProductID)
.ThenBy(p => p.ProductName)
.ThenBy(p => p.UnitPrice);

4. ThenByDescending

1>. 原型定义

public static IOrderedQueryable<TSource> ThenByDescending<TSource, TKey>(this IOrderedQueryable<TSource> source, Expression<Func<TSource, TKey>> keySelector);
public static IOrderedQueryable<TSource> ThenByDescending<TSource, TKey>(this IOrderedQueryable<TSource> source, Expression<Func<TSource, TKey>> keySelector, IComparer<TKey> comparer);

2>. 示例

var products = from p in context.Products
orderby p.ProductID, p.ProductName descending
select p;
var products = context.Products
.OrderBy(p => p.ProductID)
.ThenByDescending(p => p.ProductName)
.Select(p => p);

5. Reverse

1>.原型定义

public static IEnumerable<TSource> Reverse<TSource>(this IEnumerable<TSource> source);

2>. 示例

string[] weekdays = new string[] { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" };
var expr = from weekday in weekdays
select weekday; foreach (var item in expr.Reverse())
{
Console.WriteLine(item);
}

LINQ系列:Linq to Object排序操作符的更多相关文章

  1. LINQ系列目录

    1. LINQ准备 1.1 C#中与LINQ相关特性 2. LINQ to Object 2.1 LINQ to Object投影操作符(Select/SelectMany/Let) 2.2 LINQ ...

  2. C# ~ 从 XML 到 Linq 到 Linq to XML

    .XML 可扩展标记语言 (Extensible Markup Language), 标记 (markup) 是关键部分,是标准通用标记语言 (Standard Generalized Markup ...

  3. LINQ系列:Linq to Object集合操作符

    集合操作符对元素的集合或序列集合进行操作,并返回一个集合.LINQ共有4种集合查询操作符:Distinct.Union.Intersect和Except. 1. Distinct Distinct操作 ...

  4. LINQ操作符四:排序操作符

    Linq中的排序操作符包括OrderBy.OrderByDescending.ThenBy.ThenByDescending和Reverse,提供了升序或者降序排序. 一.OrderBy操作符 Ord ...

  5. LinQ系列文章

    温故而知新,想着系统再学习一次LinQ知识点,发现园子里有个非常棒的系列文章,所以Mark下来,方便以后查阅! 系列博客导航: LINQ之路系列博客导航 LINQ之路 1:LINQ介绍 LINQ之路 ...

  6. C# LINQ系列:LINQ to DataSet的DataTable操作 及 DataTable与Linq相互转换

    LINQ to DataSet需要使用System.Core.dll.System.Data.dll和System.Data.DataSetExtensions.dll,在项目中添加引用System. ...

  7. LINQ 系列

    C#图解教程 第十九章 LINQ   LINQ 什么是LINQLINQ提供程序 匿名类型 方法语法和查询语法查询变量查询表达式的结构 from子句join子句什么是联结查询主体中的from…let…w ...

  8. linq中的分组和排序

    一.分组  group 组内成员 by 分组条件 into 组的信息 class Program { static void Main(string[] args) { string[] name = ...

  9. LINQ 按多个字段排序(orderby、thenby、Take)

    LINQ 按多个字段排序(orderby.thenby.Take) orderby  子句解析为 OrderBy()方法,orderby descending 子句解析为OrderBy Descend ...

随机推荐

  1. css箭头

    .aui-list-item-arrow:before { content: ''; width: 0.4rem; height: 0.4rem; position: absolute; top: 5 ...

  2. Python for Infomatics 第13章 网页服务四(译)

    这几天因为其他事务,打断了自己的学习计划,今天继续我的翻译,避免又中途而废. 注:文章原文为Dr. Charles Severance 的 <Python for Informatics> ...

  3. dotnet webapi 中添加Swagger文档

    首先添加"SwaggerGenerator": "1.1.0","SwaggerUi": "1.1.0" 需要注意的是这 ...

  4. css实现一行文字居中,多行文字居左

    第一种方法: <style> *{margin:0;padding:0;} .box{width:500px;height:300px;border:1px solid #000;text ...

  5. 【PostgreSQL】PostgreSQL添加新服务器连接时,报错“Server doesn't listen ”,已解决。

    PostgreSQL添加新的服务器连接时,报错:

  6. WINFORM 输出txt文件

    SaveFileDialog saveFile1 = new SaveFileDialog(); saveFile1.Filter = "文本文件(.txt)|*.txt"; sa ...

  7. JS键盘KEYCODE值参考

    keycode 1 = 鼠标左键keycode 2 = 鼠标右键keycode 3 = Cancelkeycode 4 = 鼠标中键keycode 8 = BackSpace keycode 9 = ...

  8. CentOS7 编译安装 Git 服务器 Centos 7.0 + Git 2.2.0 + gitosis (实测 笔记)

    环境: 系统硬件:vmware vsphere (CPU:2*4核,内存2G,双网卡) 系统版本:CentOS-7.0-1406-x86_64-DVD.iso 服务器IP:192.168.1.31 域 ...

  9. 一台linux真实机实现多台Tomcat服务

    一.事前准备 ü 确保linux并未安装tomcat (这里虚拟机测试) ü 下载jdk与tomcat ① jdk-6u18-ea-bin-b01-linux-i586-20_aug_2009.bin ...

  10. MYSQL MHA

    MYSQL MHA 简介: MHA(Master High Availability)目前在MySQL高可用方面是一个相对成熟的解决方案,它由日本DeNA公司youshimaton(现就职于 Face ...