示例#1
0
        public static async Task <PagedCollection <T> > ToPagedListAsync <T>(this IQueryable <T> source, int pageIndex, int pageSize, int indexFrom = 0, CancellationToken cancellationToken = default(CancellationToken))
        {
            if (indexFrom > pageIndex)
            {
                throw new ArgumentException($"indexFrom: {indexFrom} > pageIndex: {pageIndex}, must indexFrom <= pageIndex");
            }

            var count = await source.CountAsync(cancellationToken).ConfigureAwait(false);

            var items = await source.Skip((pageIndex - indexFrom) *pageSize)
                        .Take(pageSize).ToListAsync(cancellationToken).ConfigureAwait(false);

            var pagedList = new PagedCollection <T>()
            {
                PageIndex  = pageIndex,
                PageSize   = pageSize,
                IndexFrom  = indexFrom,
                TotalCount = count
            };

            pagedList.AddRange(items);

            return(pagedList);
        }