在用Linq查詢中,常常需要用到分頁功能,因為每次都需要些分頁這些功能,於是把分頁功能提取出來,不喜大家勿噴,只是貼出來,自覺地很實用。一下貼出核心代碼: 1 /// 2 /// LinqHelper 主要用於數據集,排序 分頁等功能 3 /// 4 public c...
在用Linq查詢中,常常需要用到分頁功能,因為每次都需要些分頁這些功能,於是把分頁功能提取出來,不喜大家勿噴,只是貼出來,自覺地很實用。一下貼出核心代碼:
1 /// <summary> 2 /// LinqHelper 主要用於數據集,排序 分頁等功能 3 /// </summary> 4 public class LinqHelper 5 { 6 /// <summary> 7 /// 排序 8 /// </summary> 9 /// <typeparam name="T"></typeparam> 10 /// <param name="source"></param> 11 /// <param name="sortExpression"></param> 12 /// <param name="sortDirection"></param> 13 /// <returns></returns> 14 public static IQueryable<T> DataSorting<T>(IQueryable<T> source, string sortExpression, string sortDirection) 15 { 16 string sortingDir = string.Empty; 17 if (sortDirection.ToUpper().Trim() == "ASC") 18 sortingDir = "OrderBy"; 19 else if (sortDirection.ToUpper().Trim() == "DESC") 20 sortingDir = "OrderByDescending"; 21 ParameterExpression param = Expression.Parameter(typeof(T), sortExpression); 22 PropertyInfo pi = typeof(T).GetProperty(sortExpression); 23 Type[] types = new Type[2]; 24 types[0] = typeof(T); 25 types[1] = pi.PropertyType; 26 Expression expr = Expression.Call(typeof(Queryable), sortingDir, types, source.Expression, Expression.Lambda(Expression.Property(param, sortExpression), param)); 27 IQueryable<T> query = source.AsQueryable().Provider.CreateQuery<T>(expr); 28 return query; 29 } 30 /// <summary> 31 /// 分頁 32 /// </summary> 33 /// <typeparam name="T"></typeparam> 34 /// <param name="source"></param> 35 /// <param name="pageNumber"></param> 36 /// <param name="pageSize"></param> 37 /// <returns></returns> 38 public static IQueryable<T> DataPaging<T>(IQueryable<T> source, int pageNumber, int pageSize) 39 { 40 return source.Skip((pageNumber - 1) * pageSize).Take(pageSize); 41 } 42 /// <summary> 43 /// 排序並分頁 44 /// </summary> 45 /// <typeparam name="T"></typeparam> 46 /// <param name="source"></param> 47 /// <param name="sortExpression"></param> 48 /// <param name="sortDirection"></param> 49 /// <param name="pageNumber"></param> 50 /// <param name="pageSize"></param> 51 /// <returns></returns> 52 public static IQueryable<T> SortingAndPaging<T>(IQueryable<T> source, string sortExpression, string sortDirection, int pageNumber, int pageSize) 53 { 54 if (!string.IsNullOrEmpty(sortDirection)) 55 { 56 // IQueryable<T> query = DataSorting<T>(source, sortExpression, sortDirection); 57 } 58 IQueryable<T> query = source.AsQueryable(); 59 return DataPaging(query, pageNumber, pageSize); 60 } 61 }
在分頁中需要一個輔助的類用於分頁排序的類,用於限定分頁的參數:
1 //排序用到的參數 2 public class GridPager 3 { 4 /// <summary> 5 /// //每頁行數 6 /// </summary> 7 public int rows { get; set; } 8 /// <summary> 9 /// //當前頁是第幾頁 10 /// </summary> 11 public int page { get; set; } 12 /// <summary> 13 /// //排序方式 14 /// </summary> 15 public string order { get; set; } 16 /// <summary> 17 /// //排序列 18 /// </summary> 19 public string sort { get; set; } 20 /// <summary> 21 /// //總行數 22 /// </summary> 23 public int totalRows { get; set; } 24 }
使用方法*.ASP中 XXXX.ashx一般處理程式中,調用方法為:
1 public void GetAllInfoRW(HttpContext context) 2 { 3 string retStr = ""; 4 string rowsStr = ""; 5 int rCount = 0; 6 GridPager pager = new GridPager 7 { 8 page = Convert.ToInt32(context.Request["page"]), 9 rows = Convert.ToInt32(context.Request["rows"]), 10 sort = "RTime", 11 order = "DESC" 12 }; 13 string chk = context.Request["chk"].ToString(); 14 List<ShowCCK_DHModle> list = CCRK_CKDBiz.GetAllShowInfo(chk,ref rCount); 15 var resout = LinqHelper.SortingAndPaging(list.AsQueryable(), pager.sort, pager.sort, pager.page, pager.rows); 16 rowsStr = JsonConvert.SerializeObject(list); 17 18 retStr = "{\"total\":\"" + rCount + "\",\"rows\":" + rowsStr + "}"; 19 20 context.Response.Write(retStr); 21 }
在easyui-datagrid中註意json格式
在使用JsonConvert.SerializeObject(object); 方法中需要引入命名空間,【using Newtonsoft.Json】可以通過可以通過Nuget程式管理工具在http://www.nuget.org/官網上下載
在VS中通過Nuget程式管理工具輸入命令:Install-Package Newtonsoft.Json