zoukankan      html  css  js  c++  java
  • EF 排序扩展

    public static class LinqOrderEx
        {
            private static IOrderedQueryable<T> OrderingHelper<T>(IQueryable<T> source, string propertyName, bool descending, bool anotherLevel)
            {
                ParameterExpression param = Expression.Parameter(typeof(T), string.Empty); // I don't care about some naming
                MemberExpression property = Expression.PropertyOrField(param, propertyName);
                LambdaExpression sort = Expression.Lambda(property, param);
    
                MethodCallExpression call = Expression.Call(
                    typeof(Queryable),
                    (!anotherLevel ? "OrderBy" : "ThenBy") + (descending ? "Descending" : string.Empty),
                    new[] { typeof(T), property.Type },
                    source.Expression,
                    Expression.Quote(sort));
    
                return (IOrderedQueryable<T>)source.Provider.CreateQuery<T>(call);
            }
            public static IOrderedQueryable<T> OrderBy<T>(this IQueryable<T> source, string propertyName)
            {
                return OrderingHelper(source, propertyName, false, false);
            }
            public static IOrderedQueryable<T> OrderBy<T>(this IQueryable<T> source, string propertyName, string order)
            {
                if (order.ToLower() == "desc")
                    return OrderingHelper(source, propertyName, true, false);
                else
                    return OrderingHelper(source, propertyName, false, false);
            }
    
            public static IOrderedQueryable<T> OrderByDescending<T>(this IQueryable<T> source, string propertyName)
            {
                return OrderingHelper(source, propertyName, true, false);
            }
    
            public static IOrderedQueryable<T> ThenBy<T>(this IOrderedQueryable<T> source, string propertyName)
            {
                return OrderingHelper(source, propertyName, false, true);
            }
    
            public static IOrderedQueryable<T> ThenByDescending<T>(this IOrderedQueryable<T> source, string propertyName)
            {
                return OrderingHelper(source, propertyName, true, true);
            }
    
            public static IQueryable<T> OrderByName<T>(this IQueryable<T> q, string SortField, bool Ascending = true)
            {
                if (SortField.IndexOf("DESC") > 0)
                {
                    SortField = SortField.Split(new char[] { ' ' })[0];
                    Ascending = false;
                }
                var param = Expression.Parameter(typeof(T), "p");
                var prop = Expression.Property(param, SortField);
                var exp = Expression.Lambda(prop, param);
                string method = Ascending ? "OrderBy" : "OrderByDescending";
                Type[] types = new Type[] { q.ElementType, exp.Body.Type };
                var mce = Expression.Call(typeof(Queryable), method, types, q.Expression, exp);
                return q.Provider.CreateQuery<T>(mce);
            }
            public static IQueryable<T> OrderByName<T>(this IQueryable<T> q, string SortField, string order = "desc")
            {
                if (string.IsNullOrWhiteSpace(SortField) || string.IsNullOrWhiteSpace(order))
                {
                    return q;
                }
                if (order.ToLower() == "desc")
                {
                    return OrderByName(q, SortField, false);
                }
                else
                {
                    return OrderByName(q, SortField, true);
                }
            }
    
        }
  • 相关阅读:
    SMO学习笔记(五)——附加数据库
    SMO学习笔记(二)——还原(恢复)篇之完整恢复
    加深C# 中字符串前加@符号理解以及使用~~
    Oracle Sys用户用默认密码change_on_install 无法登录的问题(错误代码:ORA28009)
    Reflector for .NET 下载问题
    SQLSERVER拆分字符串的函数(表值函数)
    AjaxPro使用Session出错(AjaxPro "Session"引发了"System.NullReferenceException"类型的异常)
    ASP.NET Web页面(.aspx)添加用户控件(.ascx)无显示的问题
    公积金贷款与商业贷款的区别(在废打印纸中的意外收获... :))
    SMO学习笔记(三)——效验数据库备份文件
  • 原文地址:https://www.cnblogs.com/tangchun/p/7831855.html
Copyright © 2011-2022 走看看