using Furion.DependencyInjection; using Microsoft.EntityFrameworkCore; using System; using System.Collections.Generic; using System.Linq; using System.Threading; using System.Threading.Tasks; namespace YBEE.EQM.Core { [SuppressSniffer] public static class PagedUtil { /// /// 分页拓展 /// /// /// /// 页码,必须大于0 /// /// public static PageResult ToADPagedList(this IQueryable entities, int pageIndex = 1, int pageSize = 20) { if (pageIndex <= 0) throw new InvalidOperationException($"{nameof(pageIndex)} 必须是大于0的正整数。"); var totalCount = entities.Count(); var items = entities.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList(); var totalPages = (int)Math.Ceiling(totalCount / (double)pageSize); return new PageResult { PageIndex = pageIndex, PageSize = pageSize, Items = items, TotalCount = totalCount, }; } /// /// 分页拓展 /// /// /// /// 页码,必须大于0 /// /// /// public static async Task> ToADPagedListAsync(this IQueryable entities, int pageIndex = 1, int pageSize = 20, CancellationToken cancellationToken = default) { if (pageIndex <= 0) throw new InvalidOperationException($"{nameof(pageIndex)} 必须是大于0的正整数。"); var totalCount = await entities.CountAsync(cancellationToken); var items = await entities.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToListAsync(cancellationToken); var totalPages = (int)Math.Ceiling(totalCount / (double)pageSize); return new PageResult { PageIndex = pageIndex, PageSize = pageSize, Items = items, TotalCount = totalCount, }; } /// /// 分页拓展 /// /// /// /// 页码,必须大于0 /// /// public static PageResult ToADPagedList(this IEnumerable entities, int pageIndex = 1, int pageSize = 20) { if (pageIndex <= 0) throw new InvalidOperationException($"{nameof(pageIndex)} 必须是大于0的正整数。"); var totalCount = entities.Count(); var items = entities.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList(); var totalPages = (int)Math.Ceiling(totalCount / (double)pageSize); return new PageResult { PageIndex = pageIndex, PageSize = pageSize, Items = items, TotalCount = totalCount, }; } } }