zoukankan      html  css  js  c++  java
  • 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);
    }
  • 相关阅读:
    django页面分类和继承
    django前端从数据库获取请求参数
    pycharm配置django工程
    django 应用各个py文件代码
    CF. 1428G2. Lucky Numbers(背包DP 二进制优化 贪心)
    HDU. 6566. The Hanged Man(树形背包DP DFS序 重链剖分)
    小米邀请赛 决赛. B. Rikka with Maximum Segment Sum(分治 决策单调性)
    区间树 学习笔记
    CF GYM. 102861M. Machine Gun(主席树)
    2016-2017 ACM-ICPC East Central North America Regional Contest (ECNA 2016) (B, D, G, H)
  • 原文地址:https://www.cnblogs.com/libingql/p/4041394.html
Copyright © 2011-2022 走看看