Language: C#
IRepository
1: public interface IRepository<T, TPkType> 2: { 3: IQueryable<T> Select(); 4: T SelectOnId(TPkType id); 5: IQueryable<T> SelectPaged<TResult>(int pageIndex, int pageSize, Expression<Func<T, TResult>> sortExpression, bool isAscending); 6: IQueryable<T> FindPaged<TResult>(int pageIndex, int pageSize, Expression<Func<T, bool>> findExpression, Expression<Func<T, TResult>> sortExpression, bool isAscending); 7: IQueryable<T> Find(Expression<Func<T, bool>> expression); 8: T Save(T item); 9: void Delete(TPkType id); 10: } 11: 12: public interface IPagedList<T> : IList<T> 13: { 14: int PageCount { get; } 15: int TotalItemCount { get; } 16: int PageIndex { get; } 17: int PageNumber { get; } 18: int PageSize { get; } 19: bool HasPreviousPage { get; } 20: bool HasNextPage { get; } 21: bool IsFirstPage { get; } 22: bool IsLastPage { get; } 23: } 24: 25: public class SortedPagedList<T, TResult> : List<T>, IPagedList<T> 26: { 27: public int PageCount { get; private set; } 28: public int TotalItemCount { get; private set; } 29: public int PageIndex { get; private set; } 30: public int PageNumber { get { return PageIndex + 1; } } 31: public int PageSize { get; private set; } 32: public bool HasPreviousPage { get; private set; } 33: public bool HasNextPage { get; private set; } 34: public bool IsFirstPage { get; private set; } 35: public bool IsLastPage { get; private set; } 36: 37: public SortedPagedList(IEnumerable<T> source, int index, int pageSize, Expression<Func<T, TResult>> keySelector, bool asc) 38: { 39: if (source is IQueryable<T>) 40: { 41: Initialize(source as IQueryable<T>, index, pageSize, keySelector, asc); 42: } 43: else 44: { 45: Initialize(source.AsQueryable(), index, pageSize, keySelector, asc); 46: } 47: } 48: 49: public SortedPagedList(IQueryable<T> source, int index, int pageSize, Expression<Func<T, TResult>> keySelector, bool asc) 50: { 51: Initialize(source, index, pageSize, keySelector, asc); 52: } 53: 54: protected void Initialize(IQueryable<T> source, int index, int pageSize, Expression<Func<T, TResult>> keySelector, bool asc) 55: { 56: if (source == null) 57: { 58: source = new List<T>().AsQueryable(); 59: } 60: 61: TotalItemCount = source.Count(); 62: PageSize = pageSize; 63: PageIndex = index; 64: 65: if (TotalItemCount > 0) 66: { 67: PageCount = (int)Math.Ceiling(TotalItemCount / (double)PageSize); 68: } 69: else 70: { 71: PageCount = 0; 72: } 73: 74: HasPreviousPage = (PageIndex > 0); 75: HasNextPage = (PageIndex < (PageCount - 1)); 76: IsFirstPage = (PageIndex <= 0); 77: IsLastPage = (PageIndex >= (PageCount - 1)); 78: 79: if (index < 0) 80: { 81: throw new ArgumentOutOfRangeException("index", "PageIndex cannot be below 0."); 82: } 83: if (pageSize < 1) 84: { 85: throw new ArgumentOutOfRangeException("pageSize", "PageSize cannot be less than 1."); 86: } 87: 88: if (TotalItemCount > 0) 89: { 90: if (asc) 91: { 92: AddRange(source.OrderBy(keySelector).Skip(index * pageSize).Take(pageSize)); 93: } 94: else 95: { 96: AddRange(source.OrderByDescending(keySelector).Skip(index * pageSize).Take(pageSize)); 97: } 98: } 99: } 100: } 101: 102: public static class Pagination 103: { 104: public static SortedPagedList<T, TResult> ToPagedList<T, TResult>(this IQueryable<T> source, int index, int pageSize, Expression<Func<T, TResult>> keySelector, bool asc) 105: { 106: return new SortedPagedList<T, TResult>(source, index, pageSize, keySelector, asc); 107: } 108: 109: public static SortedPagedList<T, TResult> ToPagedList<T, TResult>(this IEnumerable<T> source, int index, int pageSize, Expression<Func<T, TResult>> keySelector, bool asc) 110: { 111: return new SortedPagedList<T, TResult>(source, index, pageSize, keySelector, asc); 112: } 113: }
Tags:
Report Abuse
Subscribe
Discuss
What's new
What is it
New Snippet
Recent Snippets
My Snippets
Web Code
Search

