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);
}

你可能感兴趣的:(object)