Skip to content

Commit

Permalink
Implement a paged list to return members to the client
Browse files Browse the repository at this point in the history
  • Loading branch information
marvac committed Feb 14, 2019
1 parent 87d793f commit fa5a430
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions Helpers/PagedList.cs
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);
}
}
}

0 comments on commit fa5a430

Please sign in to comment.