方案一:使用C#的List的GetRange方法实现
/// <summary>
///使用List的GetRange方法实现
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="data"></param>
/// <param name="pageIndex">起始值1开始</param>
/// <param name="pageSize"></param>
/// <returns></returns>
public static List<T> subList<T>(List<T> data, int pageNum, int pageSize)
{
//总数
var totalCount = data.Count;
//总页数算法
var totalPage = totalCount % pageSize == 0 ? totalCount / pageSize : totalCount / pageSize + 1;
var startIndex = (pageNum - 1) * pageSize;
var endIndex = pageNum * pageSize > totalCount ? totalCount - startIndex : pageSize;
var list = data.GetRange(startIndex, endIndex);
System.Diagnostics.Debug.Write(JsonHelper .Serialize(list) + "\n");
return list;
}
方案二:使用C#的List的.Skip().Take()方法
/// <summary>
/// List分页处理
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="_list"></param>
/// <param name="PageIndex"></param>
/// <param name="PageSize"></param>
/// <returns></returns>
public static List<T> SplitList<T>(List<T> _list, int PageIndex, int PageSize)
{
int _PageIndex = PageIndex == 0 ? 1 : PageIndex;
int _PageSize = PageSize == 0 ? 20 : PageSize;
int PageConut = (int)Math.Ceiling(Convert.ToDecimal(_list.Count) / _PageSize);
if (PageConut >= _PageIndex)
{
List<T> list = new List<T>();
list = _list.Skip((_PageIndex - 1) * _PageSize).Take(_PageSize).ToList();
return list;
}
else
return _list;
}