I have the following PaginatedList class.
public class PaginatedList<T> : List<T>
{
public int PageIndex { get; }
public int TotalRecords { get; }
public int TotalPages { get; }
public PaginatedList(IEnumerable<T> items, int totalRecords, int pageIndex, int pageSize)
{
PageIndex = pageIndex;
TotalRecords = totalRecords;
TotalPages = (int)Math.Ceiling(TotalRecords / (double)pageSize);
AddRange(items);
}
public bool HasPreviousPage => PageIndex > 1;
public bool HasNextPage => 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);
}
}
I'm using this class to get pagination information for list of entity that is retrieved with EF.
This is how I use this class to return list of users with pagination info.
var users = await PaginatedList<User>.CreateAsync(userQuery, pageIndex, pageSize);
Above call will return PaginatedList<User>
object.
If I have a DTO class for that entity, let's call it UserDto
. How do I use automapper to convert PaginatedList<User>
into PaginatedList<UserDto>
so the result will have all userDto objects and also the pagination info?
Otherwise, is there another/better way to achieve something similar?