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的错误,所以把这句放到了里面来执行,虽然整体代码也看不懂,但逻辑上也确实应该放到里面,至此问题解决。。。