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);
    }
  • 相关阅读:
    java工具类之按对象中某属性排序
    使用 CSS3 实现超炫的 Loading(加载)动画效果
    chrome使用技巧(看了定不让你失望)
    暗影精灵2pro——使用一年多后电池鼓包,传说中的更新BIOS问题(惠普15ax-226tx)
    【转载】 阿里面试后的问题总结
    temporal credit assignment in reinforcement learning 【强化学习 经典论文】
    【转载】 “强化学习之父”萨顿:预测学习马上要火,AI将帮我们理解人类意识
    【PPT】 Least squares temporal difference learning
    【转载】 TensorflowOnSpark:1)Standalone集群初体验
    【转载】 pytorch笔记:06)requires_grad和volatile
  • 原文地址:https://www.cnblogs.com/libingql/p/4041394.html
Copyright © 2011-2022 走看看