LinQ動態排序
首先修復程序中的一個BUG這個BUG在GridPager類中,把sord修改為sort這個名稱填寫錯誤,會導致後台一直無法獲取datagrid的排序字段
本來是沒有這一講的,為了使20行的代碼精簡成2行,我查閱了一些資料,借鑒了一些大神的建議,首先感謝第八講中,11樓@nyth和15樓@紅顏醉丶帝的建議投遞,每一次的的排序要都一個判斷這的確很麻煩,我們利用反射來解決這個問題。
先看原來的代碼
//排序 if (pager.order == "desc") { switch (pager.order) { case "CreateTime": queryData = queryData.OrderByDescending(c => c.CreateTime); break; case "Name": queryData = queryData.OrderByDescending(c => c.Name); break; default: queryData = queryData.OrderByDescending(c => c.CreateTime); break; } } else { switch (pager.order) { case "CreateTime": queryData = queryData.OrderBy(c => c.CreateTime); break; case "Name": queryData = queryData.OrderBy(c => c.Name); break; default: queryData = queryData.OrderBy(c => c.CreateTime); break; } }
以上每一次排序都要進行判斷,利用反射可以解決這個問題,我把他封裝起來了。(需要對反射有一些理解)
在App.Common中新建LinqHelper類代碼如下(下面有3個方法,我都對方法進行了注釋,看下就明白)
using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; using System.Reflection; using System.Text; using System.Threading.Tasks; namespace App.Common { public class LinqHelper { /// <summary> /// 排序 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="source"></param> /// <param name="sortExpression"></param> /// <param name="sortDirection"></param> /// <returns></returns> public static IQueryable<T> DataSorting<T>(IQueryable<T> source, string sortExpression, string sortDirection) { string sortingDir = string.Empty; if (sortDirection.ToUpper().Trim() == "ASC") sortingDir = "OrderBy"; else if (sortDirection.ToUpper().Trim() == "DESC") sortingDir = "OrderByDescending"; ParameterExpression param = Expression.Parameter(typeof(T), sortExpression); PropertyInfo pi = typeof(T).GetProperty(sortExpression); Type[] types = new Type[2]; types[0] = typeof(T); types[1] = pi.PropertyType; Expression expr = Expression.Call(typeof(Queryable), sortingDir, types, source.Expression, Expression.Lambda(Expression.Property(param, sortExpression), param)); IQueryable<T> query = source.AsQueryable().Provider.CreateQuery<T>(expr); return query; } /// <summary> /// 分頁 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="source"></param> /// <param name="pageNumber"></param> /// <param name="pageSize"></param> /// <returns></returns> public static IQueryable<T> DataPaging<T>(IQueryable<T> source, int pageNumber, int pageSize) { if (pageNumber <= 1) { return source.Take(pageSize); } else { return source.Skip((pageNumber - 1) * pageSize).Take(pageSize); } } /// <summary> /// 排序並分頁 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="source"></param> /// <param name="sortExpression"></param> /// <param name="sortDirection"></param> /// <param name="pageNumber"></param> /// <param name="pageSize"></param> /// <returns></returns> public static IQueryable<T> SortingAndPaging<T>(IQueryable<T> source, string sortExpression, string sortDirection, int pageNumber, int pageSize) { IQueryable<T> query = DataSorting<T>(source, sortExpression, sortDirection); return DataPaging(query, pageNumber, pageSize); } } }
查看本欄目