Beispiel #1
0
        /// <summary>
        /// Gets a paged list of users
        /// </summary>
        /// <param name="page">The page of users to get.</param>
        /// <param name="pageSize">Size of the page.</param>
        /// <param name="count">The number of users in the page.</param>
        /// <returns></returns>
        public IEnumerable <T> GetPagedUsers(int page, int pageSize, out long count)
        {
            // Optional parameters must appear after all other parameters
            if (page < 1)
            {
                page = 1;
            }
            if (pageSize < 1)
            {
                pageSize = 25;
            }

            MongoCursor <T> accountsQuery = _db.Users().FindAllAs <T>();

            // MongoCursor.Limit(x) restricts how many records can be retrieved
            // But MongoCursor.Skip enumerates the list, removes the first N and returns the rest as an IEnumerable<T>
            // This enumeration counts towards Limit. Once Skip is called, Limit is no longer useful.
            // Therefore to our Limit we have to add the number of records that will be skipped.
            accountsQuery.Limit = page * pageSize;
            // Count() locks the cursor, so Limit can't be called anymore
            count = accountsQuery.Count();
            IEnumerable <T> accounts = accountsQuery.Skip((page - 1) * pageSize);

            return(accounts);
        }
Beispiel #2
0
        /// <summary>
        /// Paginate mongo cursor
        /// </summary>
        /// <param name="superset">MongoCursor</param>
        /// <param name="pageNumber">Page Number</param>
        /// <param name="pageSize">Page Size</param>
        /// <exception cref="ArgumentOutOfRangeException"></exception>
        public MongoPagedList(MongoCursor <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.");
            }

            // set source to blank list if superset is null to prevent exceptions
            TotalItemCount = superset == null ? 0 : unchecked ((int)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()
                                );
            }
        }
Beispiel #3
0
 /// <summary>
 /// 对列表进行分页搜索
 /// </summary>
 /// <param name="bsonDocQuery"></param>
 /// <returns></returns>
 public List <BsonDocument> Search(int pageIndex, out int pageCount, MongoCursor <BsonDocument> bsonDocQuery)
 {
     if (bsonDocQuery == null)
     {
         pageCount = 0; return(null);
     }
     _totalPages = bsonDocQuery.Count();
     pageCount   = GetPageCount(_pageSize, _totalPages);
     if (pageIndex == 0)
     {
         pageIndex = 1;
     }
     return(bsonDocQuery.Skip(_pageSize * ((int)pageIndex - 1)).Take(_pageSize).ToList());
 }
 /// <summary>
 /// Splits a collection of objects into an unknown number of pages with n items per page (for example, if I have a list of 45 shoes and say 'shoes.Partition(10)' I will now have 4 pages of 10 shoes and 1 page of 5 shoes.
 /// </summary>
 /// <typeparam name="T">The type of object the collection should contain.</typeparam>
 /// <param name="superset">The collection of objects to be divided into subsets.</param>
 /// <param name="pageSize">The maximum number of items each page may contain.</param>
 /// <returns>A subset of this collection of objects, split into pages of maximum size n.</returns>
 public static IEnumerable <IEnumerable <T> > Partition <T>(this MongoCursor <T> superset, int pageSize)
 {
     if (superset.Count() < pageSize)
     {
         yield return(superset);
     }
     else
     {
         var numberOfPages = Math.Ceiling(superset.Count() / (double)pageSize);
         for (var i = 0; i < numberOfPages; i++)
         {
             yield return(superset.Skip(pageSize * i).Take(pageSize));
         }
     }
 }