LINQ系列:排序操作符

  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);
}
时间: 2024-07-28 14:49:37

LINQ系列:排序操作符的相关文章

LINQ操作符四:排序操作符

排序操作符,包括OrderBy.OrderByDescending.ThenBy.ThenByDescending和Reverse,提供了升序或者降序排序. OrderBy操作符将序列中的元素按照升序排列. 注意:orderby必须在select之前出现,查询表达式最后只可能出现select或者groupby. student类: 1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using Sy

LINQ系列:Linq to Object量词操作符

量词操作符返回一个Boolean值,指示序列中是否存在部分或全部元素符号指定条件.LINQ中量词操作符包括:All.Any和Contains. 1. All All操作符判定在集合中是否所有的值都满足指定的条件.返回值是一个Boolean值.如果所有值都满足条件则返回true,否则返回false. 1>. 原型定义 public static bool All<TSource>(this IEnumerable<TSource> source, Func<TSource

LINQ系列:联接操作符

联接是指将一个数据源对象与另一个数据源对象进行关联或联合的操作.这两个数据源对象通过一个共同的值或属性进行关联. LINQ的联接操作符将包含可匹配(或相同)关键字的两个或多个数据源中的值进行匹配. LINQ有两个联接操作符:join和groupjoin. 1. join join操作符类似于T-SQL中的inner join,将一个数据源与另一个数据源相联接,根据两个数据源中相等的值进行匹配. 1>. 原型定义 public static IEnumerable<TResult> Joi

LINQ标准查询操作符详解(转)

 一. 关于LINQ       LINQ 英文全称是“Language-Integrated Query”,中文为“语言集成查询”,它是微软首席架构师.Delphi 之父和C# 之父——Anders Hejlsberg 提出的并由其团队着力打造的一组用于c#和Visual Basic语言的扩展,为 C# 和 Visual Basic 语言语法提供强大的查询功能.微软从2003年开始启动LINQ的开发,在VisualStudio2008中开始加入LINQ功能. LINQ提供的便利: 1)使用一种

Linq 标准查询操作符三

本文介绍了LINQ标准查询操作符.没有这些操作符,LINQ就不会存在.本文为理解这些操作符的功能提供了很好的基础.了解它们将会很有帮助,因为LINQ的各种Provider都是基于这些操作符来完成各自丰富的功能. 一.投影操作符 1. Select Select操作符对单个序列或集合中的值进行投影.下面的示例中使用select从序列中返回Employee表的所有列: using (NorthwindDataContext db = new NorthwindDataContext()) { //查

Linq的查询操作符

Linq有表达式语法和调用方法的语法.两者是可以结合使用,通常情况下也都是结合使用.表达式语法看上去比较清晰而调用方法的语法实现的功能更多,在此文章中介绍的是表达式语法.方法语法可以看System.Linq等命名空间下的扩展方法.Linq只能用于实现了IEnumerable或IEnumerable<T>接口的类,也就是可以用foreach的类都可以用linq. 注意在3.5和4.0版本上linq的关键字有些区别,下文是以4.0为准的. 1.投影操作符 select:对集合和序列中的值进行投影.

LINQ标准查询操作符(一)——select、SelectMany、Where、OrderBy、OrderByDescending、ThenBy、ThenByDescending和Reverse

本文来自:http://blog.csdn.net/xuejianwu/article/details/6931804 一.投影操作符 1. Select Select操作符对单个序列或集合中的值进行投影.下面的示例中使用select从序列中返回Employee表的所有列: [csharp] view plaincopy using (NorthwindDataContext db=new NorthwindDataContext()) { //查询语法 var query = from e i

LINQ标准查询操作符(三)——Aggregate、Average、Distinct、Except、Intersect、Union、Empty、DefaultIfEmpty、Range、Repeat

本文来自:http://blog.csdn.net/xuejianwu/article/details/6931903 七.聚合操作符 聚合函数将在序列上执行特定的计算,并返回单个值,如计算给定序列平均值.最大值等.共有7种LINQ聚合查询操作符:Aggregate.Average.Count.LongCount.Max.Min和Sum. 1. Aggregate Aggregate操作符对集合值执行自定义聚合运算.例如,需要列出所有产品类别清单,每个类别名称之间用顿号连接.以下的代码演示了这一

LINQ系列:Linq to Object分组操作符

分组是指根据一个特定的值将序列中的值或元素进行分组.LINQ只包含一个分组操作符:GroupBy. GroupBy 1>. 原型定义 public static IQueryable<IGrouping<TKey, TSource>> GroupBy<TSource, TKey>(this IQueryable<TSource> source, Expression<Func<TSource, TKey>> keySelecto