zoukankan      html  css  js  c++  java
  • Lambda动态排序

    private static IList<T> IListOrderBy<T>(IList<T> list, string propertyName) where T : new()
            {
                if (list == null || list.Count == 0)
                {
                    return list;
                }
    
                Type elementType = Queryable.AsQueryable(list).ElementType;
                //PropertyInfo propertyInfo = elementType.GetProperty(propertyName);
                ParameterExpression parameter = Expression.Parameter(elementType, "");
                Expression body = Expression.PropertyOrField(parameter, propertyName);
    
                Expression sourceExpression = list.AsQueryable().Expression;
    
                //Type sourcePropertyType = propertyInfo.PropertyType;
                var selector = Expression.Lambda(body, parameter);
    
                Expression lambda = Expression.Call(typeof(Queryable), "OrderBy", new Type[]
                {
                    elementType,
                    selector.Body.Type
                }, sourceExpression, selector);
    
                //Expression lambda = Expression.Call(typeof(Queryable), "OrderBy", new Type[]
                //{
                //    elementType,
                //    sourcePropertyType
                //}, sourceExpression, Expression.Lambda(body, parameter));
           var query = list.AsQueryable().Provider.CreateQuery<T>(lambda);
           return query.ToList<T>(); }
    

      

  • 相关阅读:
    性能优化
    几种跨域处理
    重温前端基础之-数组去重
    移动端常遇到的问题
    WPF 应用
    WPF 应用
    WPF 应用
    C# 应用
    WPF 应用
    C# 应用
  • 原文地址:https://www.cnblogs.com/icebutterfly/p/7691250.html
Copyright © 2011-2022 走看看