Exemplo n.º 1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="PagedList{T}"/> class that divides the supplied superset into subsets the size of the supplied pageSize. The instance then only containes the objects contained in the subset specified by index.
        /// </summary>
        /// <param name="superset">The collection of objects to be divided into subsets. If the collection implements <see cref="IQueryable{T}"/>, it will be treated as such.</param>
        /// <param name="pageNumber">The one-based index of the subset of objects to be contained by this instance.</param>
        /// <param name="pageSize">The maximum size of any individual subset.</param>
        /// <exception cref="ArgumentOutOfRangeException">The specified index cannot be less than zero.</exception>
        /// <exception cref="ArgumentOutOfRangeException">The specified page size cannot be less than one.</exception>
        public PagedList(IQueryable <T> superset, int pageNumber, int pageSize, int totalRecords = 0)
        {
            if (pageNumber < 1)
            {
                throw new ArgumentOutOfRangeException("pageNumber", pageNumber, "PageNumber cannot be below 1.");
            }
            if (pageSize < 1)
            {
                throw new ArgumentOutOfRangeException("pageSize", pageSize, "PageSize cannot be less than 1.");
            }

            if (totalRecords != 0)
            {
                TotalItemCount = totalRecords;
            }
            else
            {
                // set source to blank list if superset is null to prevent exceptions
                TotalItemCount = superset == null ? 0 : superset.Count();
            }

            PageSize   = pageSize;
            PageNumber = pageNumber;
            PageCount  = TotalItemCount > 0
                        ? (int)Math.Ceiling(TotalItemCount / (double)PageSize)
                        : 0;
            HasPreviousPage = PageNumber > 1;
            HasNextPage     = PageNumber < PageCount;
            IsFirstPage     = PageNumber == 1;
            IsLastPage      = PageNumber >= PageCount;
            FirstItemOnPage = (PageNumber - 1) * PageSize + 1;
            var numberOfLastItemOnPage = FirstItemOnPage + PageSize - 1;

            LastItemOnPage = numberOfLastItemOnPage > TotalItemCount
                            ? TotalItemCount
                            : numberOfLastItemOnPage;

            // add items to internal list
            if (superset != null && TotalItemCount > 0)
            {
                Subset.AddRange(pageNumber == 1
                    ? superset.Skip(0).Take(pageSize).ToList()
                    : superset.Skip((pageNumber - 1) * pageSize).Take(pageSize).ToList()
                                );
            }
        }
Exemplo n.º 2
0
        public EntityFrameworkPagedList(IQueryable <T> superset, int pageNumber, int pageSize)
        {
            if (pageNumber < 1)
            {
                throw new ArgumentOutOfRangeException("pageNumber", pageNumber, "PageNumber cannot be below 1.");
            }
            if (pageSize < 1)
            {
                throw new ArgumentOutOfRangeException("pageSize", pageSize, "PageSize cannot be less than 1.");
            }

            var result = (pageNumber == 1
                ? superset.Skip(0).Take(pageSize)
                : superset.Skip((pageNumber - 1) * pageSize).Take(pageSize)
                          ).Future();

            // execute both queries
            // set source to blank list if superset is null to prevent exceptions
            TotalItemCount = superset == null ? 0 : superset.FutureCount();

            PageSize   = pageSize;
            PageNumber = pageNumber;
            PageCount  = TotalItemCount > 0
                        ? (int)Math.Ceiling(TotalItemCount / (double)PageSize)
                        : 0;
            HasPreviousPage = PageNumber > 1;
            HasNextPage     = PageNumber < PageCount;
            IsFirstPage     = PageNumber == 1;
            IsLastPage      = PageNumber >= PageCount;
            FirstItemOnPage = (PageNumber - 1) * PageSize + 1;
            var numberOfLastItemOnPage = FirstItemOnPage + PageSize - 1;

            LastItemOnPage = numberOfLastItemOnPage > TotalItemCount
                            ? TotalItemCount
                            : numberOfLastItemOnPage;

            // add items to internal list
            if (superset != null && TotalItemCount > 0)
            {
                Subset.AddRange(result);
            }
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="PagedList{T}"/> class that divides the supplied superset into subsets the size of the supplied pageSize. The instance then only containes the objects contained in the subset specified by index.
        /// </summary>
        /// <param name="superset">The collection of objects to be divided into subsets. If the collection implements <see cref="IQueryable{T}"/>, it will be treated as such.</param>
        /// <param name="keySelector">Expression for Order</param>
        /// <param name="pageNumber">The one-based index of the subset of objects to be contained by this instance.</param>
        /// <param name="pageSize">The maximum size of any individual subset.</param>
        /// <exception cref="ArgumentOutOfRangeException">The specified index cannot be less than zero.</exception>
        /// <exception cref="ArgumentOutOfRangeException">The specified page size cannot be less than one.</exception>
        public PagedListForEntityFramework(IQueryable <T> superset, Expression <Func <T, TKey> > keySelector, int pageNumber, int pageSize)
        {
            if (pageNumber < 1)
            {
                throw new ArgumentOutOfRangeException("pageNumber", "PageNumber cannot be below 1.");
            }
            if (pageSize < 1)
            {
                throw new ArgumentOutOfRangeException("pageSize", "PageSize cannot be less than 1.");
            }

            // set source to blank list if superset is null to prevent exceptions
            TotalItemCount = superset == null ? 0 : superset.Count();
            PageSize       = pageSize;
            PageNumber     = pageNumber;
            PageCount      = TotalItemCount > 0
                        ? (int)Math.Ceiling(TotalItemCount / (double)PageSize)
                        : 0;
            HasPreviousPage = PageNumber > 1;
            HasNextPage     = PageNumber < PageCount;
            IsFirstPage     = PageNumber == 1;
            IsLastPage      = PageNumber >= PageCount;
            FirstItemOnPage = (PageNumber - 1) * PageSize + 1;
            var numberOfLastItemOnPage = FirstItemOnPage + PageSize - 1;

            LastItemOnPage = numberOfLastItemOnPage > TotalItemCount
                            ? TotalItemCount
                            : numberOfLastItemOnPage;

            // add items to internal list
            if (superset != null && TotalItemCount > 0)
            {
                var keySelectorMethod = keySelector.Compile();

                var items = pageNumber == 1
                    ? superset.OrderBy(keySelectorMethod).Take(pageSize).ToList()
                    : superset.OrderBy(keySelectorMethod).Skip((pageNumber - 1) * pageSize).Take(pageSize).ToList();

                Subset.AddRange(items);
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// Initializes a new instance of the <see cref="PagedList{T}"/> class that divides the supplied superset into subsets the size of the supplied pageSize. The instance then only containes the objects contained in the subset specified by index.
        /// </summary>
        /// <param name="superset">The collection of objects to be divided into subsets. If the collection implements <see cref="IQueryable{T}"/>, it will be treated as such.</param>
        /// <param name="pageNumber">这个实例包含的对象子集的一个索引。</param>
        /// <param name="pageSize">任何单个子集的最大大小。</param>
        /// <exception cref="ArgumentOutOfRangeException">The specified index cannot be less than zero.</exception>
        /// <exception cref="ArgumentOutOfRangeException">The specified page size cannot be less than one.</exception>
        public PagedList(IQueryable <T> superset, int pageNumber, int pageSize)
        {
            if (pageNumber < 1)
            {
                pageNumber = 1;
            }
            if (pageSize < 1)
            {
                pageSize = 10;
            }

            // set source to blank list if superset is null to prevent exceptions
            TotalItemCount = superset == null ? 0 : superset.Count();
            PageSize       = pageSize;
            PageCount      = TotalItemCount > 0
                        ? (int)Math.Ceiling(TotalItemCount / (double)PageSize)
                        : 0;
            PageNumber      = (pageNumber == int.MaxValue) ? PageCount : pageNumber;
            HasPreviousPage = PageNumber > 1;
            HasNextPage     = PageNumber < PageCount;
            IsFirstPage     = PageNumber == 1;
            IsLastPage      = PageNumber >= PageCount;
            FirstItemOnPage = (PageNumber - 1) * PageSize + 1;
            var numberOfLastItemOnPage = FirstItemOnPage + PageSize - 1;

            LastItemOnPage = numberOfLastItemOnPage > TotalItemCount
                            ? TotalItemCount
                            : numberOfLastItemOnPage;

            // add items to internal list
            if (superset != null && TotalItemCount > 0)
            {
                Subset.AddRange(pageNumber == 1
                    ? superset.Take(pageSize).ToList()
                    : superset.Skip((pageNumber - 1) * pageSize).Take(pageSize).ToList()
                                );
            }
        }
Exemplo n.º 5
0
        /// <summary>
        /// for WCF
        /// </summary>
        public PagedList(IEnumerable <T> superset, PagedListMetaData pageMeta)
        {
            // set source to blank list if superset is null to prevent exceptions
            TotalItemCount = pageMeta.TotalItemCount;
            PageSize       = pageMeta.PageSize;
            PageNumber     = pageMeta.PageNumber;
            PageCount      = pageMeta.PageCount;

            HasPreviousPage = pageMeta.HasPreviousPage;
            HasNextPage     = PageNumber < PageCount;
            IsFirstPage     = pageMeta.IsFirstPage;
            IsLastPage      = pageMeta.IsLastPage;
            FirstItemOnPage = pageMeta.FirstItemOnPage;
            LastItemOnPage  = pageMeta.LastItemOnPage;

            // add items to internal list
            if (superset != null && TotalItemCount > 0)
            {
                Subset.AddRange(
                    superset.ToList()
                    );
            }
        }
Exemplo n.º 6
0
        /// <summary>
        /// Initializes a new instance of the <see cref="PagedList{T}"/> class that divides the supplied superset into subsets the size of the supplied pageSize. The instance then only containes the objects contained in the subset specified by index.
        /// </summary>
        /// <param name="superset">The collection of objects to be divided into subsets. If the collection implements <see cref="IQueryable{T}"/>, it will be treated as such.</param>
        /// <param name="keySelector">Expression for Order</param>
        /// <param name="pageNumber">The one-based index of the subset of objects to be contained by this instance.</param>
        /// <param name="pageSize">The maximum size of any individual subset.</param>
        /// <exception cref="ArgumentOutOfRangeException">The specified index cannot be less than zero.</exception>
        /// <exception cref="ArgumentOutOfRangeException">The specified page size cannot be less than one.</exception>
        public PagedListForEntityFramework(IQueryable <T> superset, Expression <Func <T, TKey> > keySelector, int pageNumber, int pageSize)
        {
            if (pageNumber < 1)
            {
                pageNumber = 1;
            }
            if (pageSize < 1)
            {
                PageSize = 15;
            }

            // set source to blank list if superset is null to prevent exceptions
            TotalItemCount = superset == null ? 0 : superset.Count();
            PageSize       = pageSize;
            PageNumber     = pageNumber;
            PageCount      = TotalItemCount > 0
                        ? (int)Math.Ceiling(TotalItemCount / (double)PageSize)
                        : 0;
            HasPreviousPage = PageNumber > 1;
            HasNextPage     = PageNumber < PageCount;
            IsFirstPage     = PageNumber == 1;
            IsLastPage      = PageNumber >= PageCount;
            FirstItemOnPage = (PageNumber - 1) * PageSize + 1;
            var numberOfLastItemOnPage = FirstItemOnPage + PageSize - 1;

            LastItemOnPage = numberOfLastItemOnPage > TotalItemCount
                            ? TotalItemCount
                            : numberOfLastItemOnPage;

            // add items to internal list
            if (superset != null && TotalItemCount > 0)
            {
                Subset.AddRange(pageNumber == 1
                    ? superset.Take(pageSize).ToList()
                    : superset.OrderBy(keySelector).Skip((pageNumber - 1) * pageSize).Take(pageSize).ToList());
            }
        }
Exemplo n.º 7
0
        /// <summary>
        /// Initializes a new instance of the <see cref="T:PagedList.PagedList`1"/> class that divides the supplied superset into subsets the size of the supplied pageSize. The instance then only containes the objects contained in the subset specified by index.
        ///
        /// </summary>
        /// <param name="superset">The collection of objects to be divided into subsets. If the collection implements <see cref="T:System.Linq.IQueryable`1"/>, it will be treated as such.</param><param name="pageNumber">The one-based index of the subset of objects to be contained by this instance.</param><param name="pageSize">The maximum size of any individual subset.</param><exception cref="T:System.ArgumentOutOfRangeException">The specified index cannot be less than zero.</exception><exception cref="T:System.ArgumentOutOfRangeException">The specified page size cannot be less than one.</exception>
        public SortablePagedList(IQueryable <T> superset, int pageNumber, int pageSize)
        {
            if (pageNumber < 1)
            {
                throw new ArgumentOutOfRangeException("pageNumber", pageNumber, "PageNumber cannot be below 1.");
            }
            if (pageSize < 1)
            {
                throw new ArgumentOutOfRangeException("pageSize", pageSize, "PageSize cannot be less than 1.");
            }

            var futureCount = superset.FutureCount();
            var futureData  = pageNumber == 1
                ? superset.Skip(0).Take(pageSize).Future()
                : superset.Skip((pageNumber - 1) * pageSize).Take(pageSize).Future();

            TotalItemCount  = superset == null ? 0 : futureCount.Value;
            PageSize        = pageSize;
            PageNumber      = pageNumber;
            PageCount       = TotalItemCount > 0 ? (int)Math.Ceiling(TotalItemCount / (double)PageSize) : 0;
            HasPreviousPage = PageNumber > 1;
            HasNextPage     = PageNumber < PageCount;
            IsFirstPage     = PageNumber == 1;
            IsLastPage      = PageNumber >= PageCount;
            FirstItemOnPage = (PageNumber - 1) * PageSize + 1;
            int num = FirstItemOnPage + PageSize - 1;

            LastItemOnPage = num > TotalItemCount ? TotalItemCount : num;

            if (superset == null || TotalItemCount <= 0)
            {
                return;
            }

            Subset.AddRange(futureData.ToList());
        }
Exemplo n.º 8
0
 /// <summary>
 /// Initializes a new instance of the <see cref="StaticPagedList{T}"/> class that contains the already divided subset and information about the size of the superset and the subset's position within it.
 /// </summary>
 /// <param name="subset">The single subset this collection should represent.</param>
 /// <param name="pageNumber">The one-based index of the subset of objects contained by this instance.</param>
 /// <param name="pageSize">The maximum size of any individual subset.</param>
 /// <param name="totalItemCount">The size of the superset.</param>
 /// <exception cref="ArgumentOutOfRangeException">The specified index cannot be less than zero.</exception>
 /// <exception cref="ArgumentOutOfRangeException">The specified page size cannot be less than one.</exception>
 public StaticPagedList(IEnumerable <T> subset, int pageNumber, int pageSize, int totalItemCount)
     : base(pageNumber, pageSize, totalItemCount)
 {
     Subset.AddRange(subset);
 }
Exemplo n.º 9
0
 private PagedList(IEnumerable <T> list)
 {
     Subset.AddRange(list);
 }
Exemplo n.º 10
0
 /// <summary>
 /// Used for testing purpose
 /// </summary>
 /// <param name="subsett"></param>
 public StaticPagedList(IEnumerable <T> subset)
 {
     Subset.AddRange(subset);
 }