using SqlSugar;
|
using System;
|
using System.Collections.Generic;
|
using System.Text;
|
using System.Threading.Tasks;
|
|
namespace Utility
|
{
|
public class SqlSugarPagedList
|
{
|
/// <summary>
|
/// 总条数
|
/// </summary>
|
public int Total { get; set; }
|
|
/// <summary>
|
/// 总页数
|
/// </summary>
|
public int TotalPages { get; set; }
|
|
/// <summary>
|
/// 当前页集合
|
/// </summary>
|
public object Items { get; set; }
|
}
|
/// <summary>
|
/// 分页拓展类
|
/// </summary>
|
public static class SqlSugarPagedExtensions
|
{
|
/// <summary>
|
/// 分页拓展
|
/// </summary>
|
/// <param name="query"><see cref="ISugarQueryable{TEntity}"/>对象</param>
|
/// <param name="pageIndex">当前页码,从1开始</param>
|
/// <param name="pageSize">页码容量</param>
|
/// <returns></returns>
|
public static async Task<SqlSugarPagedList> ToPagedListAsync<TEntity>(this ISugarQueryable<TEntity> query, int pageIndex, int pageSize)
|
where TEntity : new()
|
{
|
RefAsync<int> total = 0;
|
var items = await query.ToPageListAsync(pageIndex, pageSize, total);
|
var totalPages = pageSize > 0 ? (int)Math.Ceiling(total / (double)pageSize) : 0;
|
return new SqlSugarPagedList
|
{
|
Items = items,
|
Total = total,
|
TotalPages = totalPages,
|
};
|
}
|
}
|
}
|