-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathPagedListExtensions.cs
More file actions
62 lines (58 loc) · 3.18 KB
/
PagedListExtensions.cs
File metadata and controls
62 lines (58 loc) · 3.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
using System;
using System.Collections.Generic;
using System.Linq;
using MongoDB.Driver;
namespace PagedList
{
/// <summary>
/// Container for extension methods designed to simplify the creation of instances of <see cref="PagedList{T}"/>.
/// </summary>
public static class MongoPagedListExtensions
{
/// <summary>
/// Creates a subset of this collection of objects that can be individually accessed by index and containing metadata about the collection of objects the subset was created from.
/// </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. 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>
/// <returns>A subset of this collection of objects that can be individually accessed by index and containing metadata about the collection of objects the subset was created from.</returns>
/// <seealso cref="PagedList{T}"/>
public static IPagedList<T> ToPagedList<T>(this MongoCursor<T> superset, int pageNumber, int pageSize)
{
return new PagedList<T>(superset, pageNumber, pageSize);
}
/// <summary>
/// Splits a collection of objects into n pages with an (for example, if I have a list of 45 shoes and say 'shoes.Split(5)' 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="numberOfPages">The number of pages this collection should be split into.</param>
/// <returns>A subset of this collection of objects, split into n pages.</returns>
public static IEnumerable<IEnumerable<T>> Split<T>(this MongoCursor<T> superset, int numberOfPages)
{
return superset
.Select((item, index) => new { index, item })
.GroupBy(x => x.index % numberOfPages)
.Select(x => x.Select(y => y.item));
}
/// <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);
}
}
}
}