• Entity Framework 在OrderBy排序中使用字符串


        public static class LinqExtensions
        {
            private static PropertyInfo GetPropertyInfo(Type objType, string name)
            {
                var properties = objType.GetProperties();
                var matchedProperty = properties.FirstOrDefault(p => p.Name == name);
                if (matchedProperty == null)
                    throw new ArgumentException("name");
    
                return matchedProperty;
            }
            private static LambdaExpression GetOrderExpression(Type objType, PropertyInfo pi)
            {
                var paramExpr = Expression.Parameter(objType);
                var propAccess = Expression.PropertyOrField(paramExpr, pi.Name);
                var expr = Expression.Lambda(propAccess, paramExpr);
                return expr;
            }
    
            public static IEnumerable<T> OrderBy<T>(this IEnumerable<T> query, string name)
            {
                var propInfo = GetPropertyInfo(typeof(T), name);
                var expr = GetOrderExpression(typeof(T), propInfo);
    
                var method = typeof(Enumerable).GetMethods().FirstOrDefault(m => m.Name == "OrderBy" && m.GetParameters().Length == 2);
                var genericMethod = method.MakeGenericMethod(typeof(T), propInfo.PropertyType);
                return (IEnumerable<T>)genericMethod.Invoke(null, new object[] { query, expr.Compile() });
            }
    
            public static IQueryable<T> OrderBy<T>(this IQueryable<T> query, string name)
            {
                var propInfo = GetPropertyInfo(typeof(T), name);
                var expr = GetOrderExpression(typeof(T), propInfo);
    
                var method = typeof(Queryable).GetMethods().FirstOrDefault(m => m.Name == "OrderBy" && m.GetParameters().Length == 2);
                var genericMethod = method.MakeGenericMethod(typeof(T), propInfo.PropertyType);
                return (IQueryable<T>)genericMethod.Invoke(null, new object[] { query, expr });
            }
    
    
    
            public static IEnumerable<T> OrderByDescending<T>(this IEnumerable<T> query, string name)
            {
                var propInfo = GetPropertyInfo(typeof(T), name);
                var expr = GetOrderExpression(typeof(T), propInfo);
    
                var method = typeof(Enumerable).GetMethods().FirstOrDefault(m => m.Name == "OrderByDescending" && m.GetParameters().Length == 2);
                var genericMethod = method.MakeGenericMethod(typeof(T), propInfo.PropertyType);
                return (IEnumerable<T>)genericMethod.Invoke(null, new object[] { query, expr.Compile() });
            }
    
            public static IQueryable<T> OrderByDescending<T>(this IQueryable<T> query, string name)
            {
                var propInfo = GetPropertyInfo(typeof(T), name);
                var expr = GetOrderExpression(typeof(T), propInfo);
    
                var method = typeof(Queryable).GetMethods().FirstOrDefault(m => m.Name == "OrderByDescending" && m.GetParameters().Length == 2);
                var genericMethod = method.MakeGenericMethod(typeof(T), propInfo.PropertyType);
                return (IQueryable<T>)genericMethod.Invoke(null, new object[] { query, expr });
            }
        }
    

      

    基础

    //常规方法
    var query = s.OrderBy(x =>x.Age);
    
    
    //排序
    s.OrderBy(x => GetPropertyValue(x, "Age"))
    
    //函数支持
    private static object GetPropertyValue(object obj, string property)
    {
        PropertyInfo propertyInfo = obj.GetType().GetProperty(property);
        return propertyInfo.GetValue(obj, null);
    } 
    

      

  • 相关阅读:
    tc: Linux HTTP Outgoing Traffic Shaping (Port 80 Traffic Shaping)(转)
    Linux TC的ifb原理以及ingress流控-转
    插件+组件+空间
    Q查询条件
    django中的分页标签
    QuerySet
    url
    view
    HttpReponse
    装饰器
  • 原文地址:https://www.cnblogs.com/fireicesion/p/14659717.html
Copyright © 2020-2023  润新知