You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

99 lines
2.6 KiB

7 months ago
namespace Entity.Common;
/// <summary>
/// 分页组件实体类
/// </summary>
/// <typeparam name="T">泛型实体</typeparam>
[Serializable]
public class PageList<T> : List<T>, IPageList<T>
8 months ago
{
/// <summary>
7 months ago
/// 构造函数
/// </summary>
/// <param name="source">数据源</param>
/// <param name="pageIndex">分页索引</param>
/// <param name="pageSize">分页大小</param>
public PageList(IQueryable<T> source, int pageIndex, int pageSize)
{
var total = source.Count();
TotalCount = total;
TotalPages = total / pageSize;
if (total % pageSize > 0)
TotalPages++;
PageSize = pageSize;
PageIndex = pageIndex;
AddRange(source.Skip(pageIndex * pageSize).Take(pageSize).ToList());
}
/// <summary>
/// 构造函数
8 months ago
/// </summary>
7 months ago
/// <param name="source">数据源</param>
/// <param name="pageIndex">分页索引</param>
/// <param name="pageSize">分页大小</param>
public PageList(IList<T> source, int pageIndex, int pageSize)
8 months ago
{
7 months ago
TotalCount = source.Count();
TotalPages = TotalCount / pageSize;
if (TotalCount % pageSize > 0)
TotalPages++;
PageSize = pageSize;
PageIndex = pageIndex;
AddRange(source.Skip(pageIndex * pageSize).Take(pageSize).ToList());
}
/// <summary>
/// 构造函数
/// </summary>
/// <param name="source">数据源</param>
/// <param name="pageIndex">分页索引</param>
/// <param name="pageSize">分页大小</param>
/// <param name="totalCount">总记录数</param>
public PageList(IEnumerable<T> source, int pageIndex, int pageSize, int totalCount)
{
TotalCount = totalCount;
TotalPages = TotalCount / pageSize;
if (TotalCount % pageSize > 0)
TotalPages++;
PageSize = pageSize;
PageIndex = pageIndex;
AddRange(source);
8 months ago
}
7 months ago
/// <summary>
/// 分页索引
/// </summary>
public int PageIndex { get; }
/// <summary>
/// 分页大小
/// </summary>
public int PageSize { get; private set; }
/// <summary>
/// 总记录数
/// </summary>
public int TotalCount { get; }
/// <summary>
/// 总页数
/// </summary>
public int TotalPages { get; }
/// <summary>
/// 是否有上一页
/// </summary>
public bool HasPreviousPage => PageIndex > 0;
/// <summary>
/// 是否有下一页
/// </summary>
public bool HasNextPage => PageIndex + 1 < TotalPages;
}