1. 程式人生 > 實用技巧 >c# .net core List分頁加排序

c# .net core List分頁加排序

                int total = 0;
//獲取資料總數 total
= result.Count(); //判斷是否需要排序if (!string.IsNullOrEmpty(real.sortName) && (!real.sortType.ToLower().Equals("desc") || !real.sortType.ToLower().Equals("asc"))) {
//自定義接收資料實體類 List
<Returnlist> infoList = result.Skip((real.pageIndex - 1) * real.pageSize).Take(real.pageSize).ToList(); infoList.Sort( delegate (Returnlist info1, Returnlist info2) { Type t1 = info1.GetType(); Type t2
= info2.GetType(); PropertyInfo pro1 = t1.GetProperty(real.sortName); PropertyInfo pro2 = t2.GetProperty(real.sortName); string clType = pro1.PropertyType.ToString();
                 //判斷型別為數字使用數字排序,其他使用string排序
if (clType == "System.Decimal" || clType == "System.Int" || clType == "System.Int32" || clType == "System.Int64" || clType == "System.Double") { decimal info1val = Convert.ToDecimal(pro1.GetValue(info1, null)); decimal info2val = Convert.ToDecimal(pro2.GetValue(info2, null)); return real.sortType.ToLower().Equals("asc") ? info1val.CompareTo(info2val) : info2val.CompareTo(info1val); } else { return real.sortType.ToLower().Equals("asc") ? pro1.GetValue(info1, null).ToString().CompareTo(pro2.GetValue(info2, null).ToString()) : pro2.GetValue(info2, null).ToString().CompareTo(pro1.GetValue(info1, null).ToString()); } }); returnResult = new ReturnResult() { list = infoList, total = total, message = "成功" }; } else { returnResult = new ReturnResult() { list = result.Skip((real.pageIndex - 1) * real.pageSize).Take(real.pageSize).ToList(), total = branchAL.Count(), message = "成功" }; }