LINQ系列:Linq to Object排序操作符
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排序操作符的更多相关文章
- LINQ系列目录
1. LINQ准备 1.1 C#中与LINQ相关特性 2. LINQ to Object 2.1 LINQ to Object投影操作符(Select/SelectMany/Let) 2.2 LINQ ...
- C# ~ 从 XML 到 Linq 到 Linq to XML
.XML 可扩展标记语言 (Extensible Markup Language), 标记 (markup) 是关键部分,是标准通用标记语言 (Standard Generalized Markup ...
- LINQ系列:Linq to Object集合操作符
集合操作符对元素的集合或序列集合进行操作,并返回一个集合.LINQ共有4种集合查询操作符:Distinct.Union.Intersect和Except. 1. Distinct Distinct操作 ...
- LINQ操作符四:排序操作符
Linq中的排序操作符包括OrderBy.OrderByDescending.ThenBy.ThenByDescending和Reverse,提供了升序或者降序排序. 一.OrderBy操作符 Ord ...
- LinQ系列文章
温故而知新,想着系统再学习一次LinQ知识点,发现园子里有个非常棒的系列文章,所以Mark下来,方便以后查阅! 系列博客导航: LINQ之路系列博客导航 LINQ之路 1:LINQ介绍 LINQ之路 ...
- C# LINQ系列:LINQ to DataSet的DataTable操作 及 DataTable与Linq相互转换
LINQ to DataSet需要使用System.Core.dll.System.Data.dll和System.Data.DataSetExtensions.dll,在项目中添加引用System. ...
- LINQ 系列
C#图解教程 第十九章 LINQ LINQ 什么是LINQLINQ提供程序 匿名类型 方法语法和查询语法查询变量查询表达式的结构 from子句join子句什么是联结查询主体中的from…let…w ...
- linq中的分组和排序
一.分组 group 组内成员 by 分组条件 into 组的信息 class Program { static void Main(string[] args) { string[] name = ...
- LINQ 按多个字段排序(orderby、thenby、Take)
LINQ 按多个字段排序(orderby.thenby.Take) orderby 子句解析为 OrderBy()方法,orderby descending 子句解析为OrderBy Descend ...
随机推荐
- 4.3 多线程进阶篇<中>(GCD)
更正:队列名称的作用的图中,箭头标注的有些问题,已修正 本文并非最终版本,如有更新或更正会第一时间置顶,联系方式详见文末 如果觉得本文内容过长,请前往本人 “简书” 本文源码 Demo 详见 Gith ...
- 0421 & SX2016
山西省选...这个省...不算强吧...然而就是这么腊鸡题目还是wa得一无是处...怎么办啊怎么办啊...无处拯救青春和未来啊... T1: POI2004原题 BZOJ1524 n<=16.这 ...
- Python Turtle
之前对这个turtle这个模块不了解,觉得没什么意思,最近试了一下发现不错,来点最简单的.写的时候深刻感受到自己智商是个硬伤,算角度都算了半天... 图就不导了吧,懒癌晚期... import tur ...
- bootstrap之HTML模板
bootstrap之HTML模板 <!DOCTYPE html> <html> <head> <title>Bootstrap 模板</title ...
- 关于zepto(相似于jquery、jQuery用于网页浏览器,zepto用于手机浏览器)
http://blog.csdn.net/kongjiea/article/details/42522305 -----关于zepto和jquery的差别 jQuery 使用 .width() 和 ...
- 【Postgresql】数据库函数
1.Postgresql查询前几条记录的SQL语句 select * from table where ...... LIMIT N ; 2.SQL limit integer offset int ...
- jsp 中 有没有类似java if else语句
<c:when test=""></c:when> <c:otherwise></c:otherwise> 有if else的功能 ...
- iOS 属性修饰符记录 --不定时更新
重新审视了一下OC在属性修饰符,特意记录一下来.以后不定时更新 > retain:只有在非ARC下才会有效,所有如果在ARC下使用了retain修饰也白搭 如以下的data属性用retain修饰 ...
- mysql不能插入中文
mysql不能插入中文 解决办法: 1.打开终端,连接数据库 mysql -u root -p; 2.输入 satus; 查看状态 3.输入 set char set 'gbk'; 4.如果是已有的 ...
- CentOS 安装 gcc-c++
由于网络环境的问题,很难在线安装,可以 直接挂载安装盘文件,然后在Pacakage 中进行本地安装