zoukankan      html  css  js  c++  java
  • LINQ 根据指定属性名称对序列进行排序

            /// <summary>
            /// 根据指定属性名称对序列进行排序
            /// </summary>
            /// <typeparam name="TSource">source中的元素的类型</typeparam>
            /// <param name="source">一个要排序的值序列</param>
            /// <param name="property">属性名称</param>
            /// <param name="descending">是否降序</param>
            /// <returns></returns>
            public static IQueryable<TSource> OrderBy<TSource>(this IQueryable<TSource> source, string property, bool descending) where TSource : class
            {
                ParameterExpression param = Expression.Parameter(typeof(TSource), "c");
                PropertyInfo pi = typeof(TSource).GetProperty(property);
                MemberExpression selector = Expression.MakeMemberAccess(param, pi);
                LambdaExpression le = Expression.Lambda(selector, param);
                string methodName = (descending) ? "OrderByDescending" : "OrderBy";
                MethodCallExpression resultExp = Expression.Call(typeof(Queryable), methodName, new Type[] { typeof(TSource), pi.PropertyType }, source.Expression, le);
                return source.Provider.CreateQuery<TSource>(resultExp);
            }
    

      

  • 相关阅读:
    PHP中的list(),each(),reset()函数应用
    echo(),print(),print_r()
    Math.floor() 与 parseInt()
    利用Node.js轻松创建web服务器
    MySQL中Datetime与Timestamp
    修正正则匹配日期---基于网络未知大神的正则
    数据结构随笔-php实现栈
    数据结构随笔-php实现队列
    Js 获取时间戳
    linux 安装nginx+php+mysql
  • 原文地址:https://www.cnblogs.com/itclw/p/12737142.html
Copyright © 2011-2022 走看看