您的位置:首页 > 产品设计 > UI/UE

IQueryable定义一个扩展方法。分页

2012-05-16 21:26 369 查看
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace page
{
public class PagedList<T> : List<T>
{
public PagedList(IEnumerable<T> content, int currentPage, int pageSize, int totalCount)
: this(totalCount, currentPage, pageSize)
{
AddRange(content);
}
public PagedList(IQueryable<T> source, int currentPage, int pageSize)
: this(source.Count(), currentPage, pageSize)
{
AddRange(source.Skip((CurrentPage - 1) * PageSize).Take(PageSize).ToList());
}

public PagedList(IEnumerable<T> source, int currentPage, int pageSize)
: this(source.Count(), currentPage, pageSize)
{
AddRange(source.Skip((CurrentPage - 1) * PageSize).Take(PageSize).ToList());
}

protected PagedList(int count, int currentPage, int pageSize)
{
TotalCount = count;
PageSize = Math.Max(pageSize, 1);
CurrentPage = Math.Min(Math.Max(currentPage, 1), TotalPages);
}

public int CurrentPage { get; set; }

public bool HasPreviousPage
{
get { return CurrentPage > 1; }
}

public bool HasNextPage
{
get { return CurrentPage < TotalPages; }
}

public int PageSize { get; set; }

public int TotalCount { get; set; }

public int TotalPages
{
get { return Math.Max((TotalCount + PageSize - 1) / PageSize, 1); }
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐