schangxiang@126.com
2024-09-04 9242d0da6005e070b0197a26b12ba24d1361466d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
using Furion.DependencyInjection;
using Microsoft.EntityFrameworkCore;
 
namespace iWare.Wms.Core
{
    [SuppressSniffer]
    public static class PagedUtil
    {
        /// <summary>
        /// 分页拓展
        /// </summary>
        /// <typeparam name="TEntity"></typeparam>
        /// <param name="entities"></param>
        /// <param name="pageIndex">页码,必须大于0</param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public static PageResult<TEntity> ToADPagedList<TEntity>(this IQueryable<TEntity> 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<TEntity>
            {
                PageNo = pageIndex,
                PageSize = pageSize,
                Rows = items,
                TotalRows = totalCount,
                TotalPage = totalPages
            };
        }
 
        /// <summary>
        /// 分页拓展
        /// </summary>
        /// <typeparam name="TEntity"></typeparam>
        /// <param name="entities"></param>
        /// <param name="pageIndex">页码,必须大于0</param>
        /// <param name="pageSize"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public static async Task<PageResult<TEntity>> ToADPagedListAsync<TEntity>(this IQueryable<TEntity> entities, int pageIndex = 1, int pageSize = 20, CancellationToken cancellationToken = default)
        {
            if (pageIndex <= 0) throw new InvalidOperationException($"{nameof(pageIndex)} 必须是大于0的正整数。");
 
            try
            {
                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<TEntity>
                {
                    PageNo = pageIndex,
                    PageSize = pageSize,
                    Rows = items,
                    TotalRows = totalCount,
                    TotalPage = totalPages
                };
            }
            catch (Exception ex)
            {
 
                return new PageResult<TEntity>
                {
                    PageNo = 1,
                    PageSize = 1,
                    Rows = new List<TEntity>(),
                    TotalRows = 0,
                    TotalPage = 1
                };
            }
 
        }
 
        /// <summary>
        /// 分页拓展
        /// </summary>
        /// <typeparam name="TEntity"></typeparam>
        /// <param name="entities"></param>
        /// <param name="pageIndex">页码,必须大于0</param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public static PageResult<TEntity> ToADPagedList<TEntity>(this IEnumerable<TEntity> 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<TEntity>
            {
                PageNo = pageIndex,
                PageSize = pageSize,
                Rows = items,
                TotalRows = totalCount,
                TotalPage = totalPages
            };
        }
    }
}