using System;
using System.Collections.Generic;
using System.Linq;
namespace PalGain.Core
{
///
/// Paged list
///
/// T
[Serializable]
public class PagedList : List, IPagedList
{
public PagedList() { }
///
/// Ctor
///
/// source
/// Page index
/// Page size
public PagedList(IQueryable source, int pageIndex, int pageSize)
{
int total = source.Count();
this.TotalCount = total;
this.TotalPages = total / pageSize;
if (total % pageSize > 0)
TotalPages++;
this.PageSize = pageSize;
if (pageIndex > TotalPages)
{
PageIndex = TotalPages;
}
else
{
this.PageIndex = pageIndex;
}
this.AddRange(source.Skip((PageIndex <= 0 ? 0 : PageIndex - 1) * pageSize).Take(pageSize).ToList());
}
///
/// Ctor
///
/// source
/// Page index
/// Page size
public PagedList(IList source, int pageIndex, int pageSize)
{
TotalCount = source.Count();
TotalPages = TotalCount / pageSize;
if (TotalCount % pageSize > 0)
TotalPages++;
this.PageSize = pageSize;
this.PageIndex = pageIndex;
this.AddRange(source.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList());
}
///
/// Ctor
///
/// source
/// Page index
/// Page size
/// Total count
public PagedList(IEnumerable source, int pageIndex, int pageSize, int totalCount)
{
TotalCount = totalCount;
TotalPages = TotalCount / pageSize;
if (TotalCount % pageSize > 0)
TotalPages++;
this.PageSize = pageSize;
this.PageIndex = pageIndex;
this.AddRange(source);
}
public int PageIndex { get; private set; }
public int PageSize { get; private set; }
public int TotalCount { get; private set; }
public int TotalPages { get; private set; }
public bool HasPreviousPage
{
get { return (PageIndex > 0); }
}
public bool HasNextPage
{
get { return (PageIndex + 1 < TotalPages); }
}
}
}