LINQ系列:Linq to Object排序操作符

libingql發表於2014-10-21

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

相關文章