sexta-feira, 17 de julho de 2015

PaginatedData

using System;
using System.Collections.Generic;
using System.Linq;

namespace Lapuinka.Web
{
    public class PaginatedData : List
    {
        public int PageIndex { get; set; }
        public int PageSize { get; set; }
        public int TotalCount { get; set; }
        public int TotalPages { get; set; }

        public string OrderBy { get; set; }
        public string OrderAsc { get; set; }


        public PaginatedData(IQueryable source, int pageIndex, int pageSize, int totalCount, string sort, string sortAsc)
        {
            PageIndex = pageIndex;
            PageSize = pageSize;
            TotalCount = totalCount;
            TotalPages = (int)Math.Ceiling(TotalCount / (double)PageSize);
            OrderBy = sort;
            OrderAsc = sortAsc;

            this.AddRange(source);
        }


        public PaginatedData(IQueryable source, int pageIndex, int pageSize, int totalCount)
        {
            PageIndex = pageIndex;
            PageSize = pageSize;
            TotalCount = totalCount;

            TotalPages = (int)Math.Ceiling(TotalCount / (double)PageSize);

            this.AddRange(source);
        }

        public PaginatedData(IQueryable source, int pageIndex, int pageSize)
        {
            PageIndex = pageIndex;
            PageSize = pageSize;
            TotalCount = source.Count();

            TotalPages = (int)Math.Ceiling(TotalCount / (double)PageSize);

            this.AddRange(source.Skip((PageIndex - 1) * PageSize).Take(PageSize));
        }
        public bool HasPreviousPage
        {
            get
            {
                return (PageIndex > 1);
            }
        }
        public bool HasNextPage
        {
            get
            {
                return (PageIndex < TotalPages);
            }
        }
    }
}



Postar um comentário