代码改变世界

A PageList: A page control on WPF

2012-01-09 10:57  barbarossia  阅读(358)  评论(0编辑  收藏  举报

I create a page control used to data access and display a page which own defined. It used the entityframework4.1 as data access and IQuerable<T> handle the page it's mechism.

The code is:

   public class PagedList<T> : List<T> {
public int StartIndex { get; private set; }
public int PageSize { get; private set; }
public int TotalCount { get; private set; }
public int PageCount {
get {
if ((TotalCount % PageSize) > 0)
return (TotalCount / PageSize) + 1;
else
return TotalCount / PageSize;
}
}

private PagedList(IEnumerable<T> pagedList, int startIndex, int pageSize, int totalCount)
: base(pagedList) {
StartIndex = startIndex;
PageSize = pageSize;
TotalCount = totalCount;
}

public PagedList(IQueryable<T> dataSource, int startIndex, int pageSize)
: base(dataSource.Skip(startIndex).Take(pageSize)) {
StartIndex = startIndex;
PageSize = pageSize;
TotalCount = dataSource.Count();
}

public PagedList<TResult> Transform<TResult>(Func<T, TResult> selector) {
return new PagedList<TResult>(this.Select(selector), StartIndex, PageSize, TotalCount);
}
}