• EF OrderBy(string propertyname), OrderByDescending(string propertyname) 按属性排序,扩展方法


      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 });
            }
        }
    

     YourDbContext db = new  YourDbContext();

    这样就可以直接使用下面这个方法了, db.TableName.OrderBy("ColumnName"), 用于动态排序时需要

  • 相关阅读:
    《30天自制操作系统》笔记(10)——定时器
    《30天自制操作系统》笔记(09)——绘制窗口
    《30天自制操作系统》笔记(08)——叠加窗口刷新
    《30天自制操作系统》笔记(07)——内存管理
    《30天自制操作系统》笔记(06)——CPU的32位模式
    《30天自制操作系统》笔记(05)——启用鼠标键盘
    《30天自制操作系统》笔记(04)——显示器256色
    《30天自制操作系统》笔记(03)——使用Vmware
    《30天自制操作系统》笔记(02)——导入C语言
    《30天自制操作系统》笔记(01)——hello bitzhuwei’s OS!
  • 原文地址:https://www.cnblogs.com/sgciviolence/p/5630584.html
Copyright © 2020-2023  润新知