namespace SharedDATA.Context
{
using System;
using System.Collections.Generic;
using System.Linq;
///
/// Represents the default implementation of the interface.
///
/// The type of the data to page 页类型的数据
public class PagedList : IPagedList
{
///
/// Gets or sets the index of the page.
/// 获得页的起始页
///
/// The index of the page.
public int PageIndex { get; set; }
///
/// Gets or sets the size of the page.
/// 获得页大小
///
/// The size of the page.
public int PageSize { get; set; }
///
/// Gets or sets the total count.
/// 获得总数
///
/// The total count.
public int TotalCount { get; set; }
///
/// Gets or sets the total pages.
/// 获得总页数
///
/// The total pages.
public int TotalPages { get; set; }
///
/// Gets or sets the index from.
/// 从索引起
///
/// The index from.
public int IndexFrom { get; set; }
///
/// Gets or sets the items.
/// 数据
///
/// The items.
public IList Items { get; set; }
///
/// Gets the has previous page.
/// 获取前一页
///
/// The has previous page.
public bool HasPreviousPage => PageIndex - IndexFrom > 0;
///
/// Gets the has next page.
/// 获取下一页
///
/// The has next page.
public bool HasNextPage => PageIndex - IndexFrom + 1 < TotalPages;
///
/// Initializes a new instance of the class.
///
/// The source.
/// The index of the page.
/// The size of the page.
/// The index from.
public PagedList(IEnumerable source, int pageIndex, int pageSize, int indexFrom)
{
if (indexFrom > pageIndex)
{
throw new ArgumentException($"indexFrom: {indexFrom} > pageIndex: {pageIndex}, must indexFrom <= pageIndex");
}
if (source is IQueryable querable)
{
PageIndex = pageIndex;
PageSize = pageSize;
IndexFrom = indexFrom;
TotalCount = querable.Count();
TotalPages = (int)Math.Ceiling(TotalCount / (double)PageSize);
Items = querable.Skip((PageIndex - IndexFrom) * PageSize).Take(PageSize).ToList();
}
else
{
PageIndex = pageIndex;
PageSize = pageSize;
IndexFrom = indexFrom;
TotalCount = source.Count();
TotalPages = (int)Math.Ceiling(TotalCount / (double)PageSize);
Items = source.Skip((PageIndex - IndexFrom) * PageSize).Take(PageSize).ToList();
}
}
///
/// Initializes a new instance of the class.
///
public PagedList() => Items = new T[0];
}
///
/// Provides the implementation of the and converter.
///
/// The type of the source.
/// The type of the result.
public class PagedList : IPagedList
{
///
/// Gets the index of the page.
///
/// The index of the page.
public int PageIndex { get; }
///
/// Gets the size of the page.
///
/// The size of the page.
public int PageSize { get; }
///
/// Gets the total count.
///
/// The total count.
public int TotalCount { get; }
///
/// Gets the total pages.
///
/// The total pages.
public int TotalPages { get; }
///
/// Gets the index from.
///
/// The index from.
public int IndexFrom { get; }
///
/// Gets the items.
///
/// The items.
public IList Items { get; }
///
/// Gets the has previous page.
///
/// The has previous page.
public bool HasPreviousPage => PageIndex - IndexFrom > 0;
///
/// Gets the has next page.
///
/// The has next page.
public bool HasNextPage => PageIndex - IndexFrom + 1 < TotalPages;
///
/// Initializes a new instance of the class.
///
/// The source.
/// The converter.
/// The index of the page.
/// The size of the page.
/// The index from.
public PagedList(IEnumerable source, Func, IEnumerable> converter, int pageIndex, int pageSize, int indexFrom)
{
if (indexFrom > pageIndex)
{
throw new ArgumentException($"indexFrom: {indexFrom} > pageIndex: {pageIndex}, must indexFrom <= pageIndex");
}
if (source is IQueryable querable)
{
PageIndex = pageIndex;
PageSize = pageSize;
IndexFrom = indexFrom;
TotalCount = querable.Count();
TotalPages = (int)Math.Ceiling(TotalCount / (double)PageSize);
var items = querable.Skip((PageIndex - IndexFrom) * PageSize).Take(PageSize).ToArray();
Items = new List(converter(items));
}
else
{
PageIndex = pageIndex;
PageSize = pageSize;
IndexFrom = indexFrom;
TotalCount = source.Count();
TotalPages = (int)Math.Ceiling(TotalCount / (double)PageSize);
var items = source.Skip((PageIndex - IndexFrom) * PageSize).Take(PageSize).ToArray();
Items = new List(converter(items));
}
}
///
/// Initializes a new instance of the class.
///
/// The source.
/// The converter.
public PagedList(IPagedList source, Func, IEnumerable> converter)
{
PageIndex = source.PageIndex;
PageSize = source.PageSize;
IndexFrom = source.IndexFrom;
TotalCount = source.TotalCount;
TotalPages = source.TotalPages;
Items = new List(converter(source.Items));
}
}
///
/// Provides some help methods for interface.
///
public static class PagedList
{
///
/// Creates an empty of .
///
/// The type for paging
/// An empty instance of .
public static IPagedList Empty() => new PagedList();
///
/// Creates a new instance of from source of instance.
///
/// The type of the result.
/// The type of the source.
/// The source.
/// The converter.
/// An instance of .
public static IPagedList From(IPagedList source, Func, IEnumerable> converter) => new PagedList(source, converter);
}
}