我有以下PaginatedList类。
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);
}
}
我使用此类来获取使用 EF 检索的实体列表的分页信息。
这就是我使用此类返回具有分页信息的用户列表的方式。
var users = await PaginatedList<User>.CreateAsync(userQuery, pageIndex, pageSize);
以上调用将返回PaginatedList<User>
对象。
如果我有该实体的 DTO 类,我们将其称为 UserDto
。如何使用automapper转换PaginatedList<User>
进入PaginatedList<UserDto>
所以结果将包含所有 userDto 对象以及分页信息?
否则,是否有另一种/更好的方法来实现类似的目标?
最佳答案
我最终在 PaginatedList 中创建了另一个工厂方法来处理转换。
public static async Task<PaginatedList<TResult>> CreateAsync<TSource>(
IQueryable<TSource> source, int pageIndex, int pageSize)
{
Ensure.It.IsGreaterThan(0, pageIndex);
Ensure.It.IsGreaterThan(0, pageSize);
var count = await source.CountAsync();
var items = await source.Skip((pageIndex - 1) * pageSize)
.Take(pageSize)
.Select(ufc => Mapper.Map<TResult>(ufc))
.ToListAsync();
return new PaginatedList<TResult>(items, count, pageIndex, pageSize);
}
然后将现有的CreateAsync
调用这个新方法 CreateAsync<TSource>
方法。
public static async Task<PaginatedList<TResult>> CreateAsync(
IQueryable<TResult> source, int pageIndex, int pageSize)
{
return await CreateAsync<TResult>(source, pageIndex, pageSize);
}
有了这个,如果我们想继续返回相同的实体类,我们可以这样使用它
await PaginatedList<User>.CreateAsync(_dbContext.User.AsQueryable(), pageIndex, pageSize)
如果我们想将实体类转换为返回Dto类或者其他类,可以这样使用
await PaginatedList<UserDto>.CreateAsync(_dbContext.User.AsQueryable(), pageIndex, pageSize)
如果我们不将实体转换为其他类,则不需要在自动映射器配置中指定任何内容。但如果你想将实体映射到其他类,那么需要在自动映射器中进行配置。
或多或少,PaginatedList 类如下所示
public class PaginatedList<TResult> : List<TResult>
{
public int PageIndex { get; }
public int TotalRecords { get; }
public int TotalPages { get; }
public PaginatedList(IEnumerable<TResult> items, int count, int pageIndex, int pageSize)
{
PageIndex = pageIndex;
TotalRecords = count;
TotalPages = (int)Math.Ceiling(TotalRecords / (double)pageSize);
AddRange(items);
}
public bool HasPreviousPage => PageIndex > 1;
public bool HasNextPage => PageIndex < TotalPages;
public static async Task<PaginatedList<TResult>> CreateAsync<TSource>(
IQueryable<TSource> source, int pageIndex, int pageSize)
{
var count = await source.CountAsync();
var items = await source.Skip((pageIndex - 1) * pageSize)
.Take(pageSize)
.Select(ufc => Mapper.Map<TResult>(ufc))
.ToListAsync();
return new PaginatedList<TResult>(items, count, pageIndex, pageSize);
}
public static async Task<PaginatedList<TResult>> CreateAsync(
IQueryable<TResult> source, int pageIndex, int pageSize)
{
return await CreateAsync<TResult>(source, pageIndex, pageSize);
}
}
关于c# - 将实体分页类映射到 dto 分页类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54015297/