-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement a paged list to return members to the client
- Loading branch information
Showing
1 changed file
with
34 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
using Microsoft.EntityFrameworkCore; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
|
||
namespace Friendster.Helpers | ||
{ | ||
public class PagedList<T> : List<T> | ||
{ | ||
public int CurrentPage { get; set; } | ||
public int TotalPages { get; set; } | ||
|
||
public int TotalCount { get; set; } | ||
public int PageSize { get; set; } | ||
|
||
public PagedList(List<T> items, int count, int currentPage, int pageSize) | ||
{ | ||
TotalCount = count; | ||
CurrentPage = currentPage; | ||
PageSize = pageSize; | ||
TotalPages = (int)Math.Ceiling(count / (double)pageSize); | ||
|
||
this.AddRange(items); | ||
} | ||
|
||
public static async Task<PagedList<T>> CreateAsync(IQueryable<T> source, int currentPage, int pageSize) | ||
{ | ||
int count = await source.CountAsync(); | ||
var items = await source.Skip((currentPage - 1) * pageSize).Take(pageSize).ToListAsync(); | ||
return new PagedList<T>(items, count, currentPage, pageSize); | ||
} | ||
} | ||
} |