-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathPaginatedList.linq
53 lines (44 loc) · 1.2 KB
/
PaginatedList.linq
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
<Query Kind="Program">
<NuGetReference>Microsoft.EntityFrameworkCore</NuGetReference>
<Namespace>Microsoft.EntityFrameworkCore</Namespace>
<Namespace>System.Threading.Tasks</Namespace>
</Query>
void Main()
{
var list = new List<int> {1, 2, 3, 4, 5, 6, 7, 8, 9};
var paginated = new PaginatedList<int>(list, 10, 2, 2);
paginated.HasPreviousPage.Dump();
paginated.HasNextPage.Dump();
paginated.TotalPages.Dump();
}
public class PaginatedList<T> : List<T>
{
public int PageIndex { get; private set; }
public int TotalPages { get; private set; }
public PaginatedList(List<T> items, int count, int pageIndex, int pageSize)
{
PageIndex = pageIndex;
TotalPages = (int)Math.Ceiling(count / (double)pageSize);
this.AddRange(items);
}
public bool HasPreviousPage
{
get
{
return (PageIndex > 1);
}
}
public bool HasNextPage
{
get
{
return (PageIndex < TotalPages);
}
}
public static async Task<PaginatedList<T>> CreateAsync(IQueryable<T> source, int pageIndex, int pageSize)
{
var count = await source.CountAsync();
var items = await source.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToListAsync();
return new PaginatedList<T>(items, count, pageIndex, pageSize);
}
}