zoukankan      html  css  js  c++  java
  • NFine完善列表页多字段排序

    问题描述:之前尝试列表页定义两个排序字段你,但是总按照最后面的字段来,并没有按照多字段排序。。。

    解决方案:昨晚深入琢磨了半天,找到了排序的相关代码

            public async Task<List<T>> FindList(Expression<Func<T, bool>> predicate, Pagination pagination)
            {
                bool isAsc = pagination.sord.ToLower() == "asc" ? true : false;
                string[] _order = pagination.sidx.Split(',');
                MethodCallExpression resultExp = null;
                var tempData = Repository.Find(predicate);
                foreach (string item in _order)
                {
                    string _orderPart = item;
                    _orderPart = Regex.Replace(_orderPart, @"s+", " ");
                    string[] _orderArry = _orderPart.Split(' ');
                    string _orderField = _orderArry[0];
                    bool sort = isAsc;
                    if (_orderArry.Length == 2)
                    {
                        isAsc = _orderArry[1].ToUpper() == "ASC" ? true : false;
                    }
                    var parameter = Expression.Parameter(typeof(T), "t");
                    var property = typeof(T).GetProperty(_orderField);
                    var propertyAccess = Expression.MakeMemberAccess(parameter, property);
                    var orderByExp = Expression.Lambda(propertyAccess, parameter);
                    resultExp = Expression.Call(typeof(Queryable), isAsc ? "OrderBy" : "OrderByDescending", new Type[] { typeof(T), property.PropertyType }, tempData.Expression, Expression.Quote(orderByExp));
                }
                tempData = tempData.Provider.CreateQuery<T>(resultExp);
                pagination.records = tempData.Count();
                tempData = tempData.Skip<T>(pagination.rows * (pagination.page - 1)).Take<T>(pagination.rows).AsQueryable();
                return await tempData.ToListAsync();
            }

    可以看到是允许传入多排序参数的,而且代码里也做了相关处理,结合起来linq中两个orderby肯定会执行最后一次,这个地方是欠缺使用thenby,所以整改起来

            public async Task<List<T>> FindList(Expression<Func<T, bool>> predicate, Pagination pagination)
            {
                bool isAsc = pagination.sord.ToLower() == "asc" ? true : false;
                string[] _order = pagination.sidx.Split(',');
                MethodCallExpression resultExp = null;
                var tempData = Repository.Find(predicate);
                var tempCurrent = 0;
                foreach (string item in _order)
                {
                    string _orderPart = item;
                    _orderPart = Regex.Replace(_orderPart, @"s+", " ");
                    string[] _orderArry = _orderPart.Split(' ');
                    string _orderField = _orderArry[0];
                    bool sort = isAsc;
                    if (_orderArry.Length == 2)
                    {
                        isAsc = _orderArry[1].ToUpper() == "ASC" ? true : false;
                    }
                    var parameter = Expression.Parameter(typeof(T), "t");
                    var property = typeof(T).GetProperty(_orderField);
                    var propertyAccess = Expression.MakeMemberAccess(parameter, property);
                    var orderByExp = Expression.Lambda(propertyAccess, parameter);
                    if (tempCurrent == 0)
                    {
                        resultExp = Expression.Call(typeof(Queryable), isAsc ? "OrderBy" : "OrderByDescending", new Type[] { typeof(T), property.PropertyType }, tempData.Expression, Expression.Quote(orderByExp));
                    }
                    else
                    {
                        resultExp = Expression.Call(typeof(Queryable), isAsc ? "ThenBy" : "ThenByDescending", new Type[] { typeof(T), property.PropertyType }, tempData.Expression, Expression.Quote(orderByExp));
                    }
                    tempData = tempData.Provider.CreateQuery<T>(resultExp);
                    tempCurrent++;
                }
                pagination.records = tempData.Count();
                tempData = tempData.Skip<T>(pagination.rows * (pagination.page - 1)).Take<T>(pagination.rows).AsQueryable();
                return await tempData.ToListAsync();
            }

    黄色标识的代码是重点,如果放到循环外面会报错,错误代码如下:

    No generic method 'ThenByDescending' on type 'System.Linq.Queryable' is comp...

    意思大概是没有执行orderby而直接执行thenby的错误,所以把这句放到了里面来执行,虽然整体代码也看不懂,但逻辑上也确实应该放到里面,至此问题解决。。。

  • 相关阅读:
    xtu summer individual 6 B
    Docker和宿主机操作系统文件目录互相隔离的实现原理
    SAP成都研究院飞机哥: SAP C4C中国本地化之微信聊天机器人的集成
    C4C销售订单行项目价格维护方法
    Jerry Wang诚邀广大SAP同仁免费加入我的知识星球,共同探讨SAP技术问题
    为什么CRM Opportunity的删除会触发一个通向BW系统的RFC
    如何用代码填充S/4HANA销售订单行项目的数量字段
    SAP成都研究院大卫哥:SAP C4C中国本地化之微信小程序集成
    如何获得C4C里某个code字段对应的描述信息
    如何用代码的方式取出SAP C4C销售订单创建后所有业务伙伴的数据
  • 原文地址:https://www.cnblogs.com/wangbg/p/11216933.html
Copyright © 2011-2022 走看看