From 9897a0e5c50dde4750285ebbc709e3f7f7cfecc9 Mon Sep 17 00:00:00 2001 From: sunkaixuan <610262374@qq.com> Date: Wed, 13 Apr 2022 01:07:43 +0800 Subject: [PATCH] Add ForEachByPageAsync --- .../QueryableProvider/QueryableProvider.cs | 37 +++++++++++++++++++ Src/Asp.Net/SqlSugar/Interface/IQueryable.cs | 1 + 2 files changed, 38 insertions(+) diff --git a/Src/Asp.Net/SqlSugar/Abstract/QueryableProvider/QueryableProvider.cs b/Src/Asp.Net/SqlSugar/Abstract/QueryableProvider/QueryableProvider.cs index 481693795..b953df819 100644 --- a/Src/Asp.Net/SqlSugar/Abstract/QueryableProvider/QueryableProvider.cs +++ b/Src/Asp.Net/SqlSugar/Abstract/QueryableProvider/QueryableProvider.cs @@ -1477,6 +1477,43 @@ namespace SqlSugar } totalNumber = count; } + public virtual async Task ForEachByPageAsync(Action action, int pageIndex, int pageSize,RefAsync totalNumber, int singleMaxReads = 300, System.Threading.CancellationTokenSource cancellationTokenSource = null) + { + int count = this.Clone().Count(); + if (count > 0) + { + if (pageSize > singleMaxReads && count - ((pageIndex - 1) * pageSize) > singleMaxReads) + { + Int32 Skip = (pageIndex - 1) * pageSize; + Int32 NowCount = count - Skip; + Int32 number = 0; + if (NowCount > pageSize) NowCount = pageSize; + while (NowCount > 0) + { + if (cancellationTokenSource?.IsCancellationRequested == true) return; + if (number + singleMaxReads > pageSize) singleMaxReads = NowCount; + foreach (var item in await this.Clone().Skip(Skip).Take(singleMaxReads).ToListAsync()) + { + if (cancellationTokenSource?.IsCancellationRequested == true) return; + action.Invoke(item); + } + NowCount -= singleMaxReads; + Skip += singleMaxReads; + number += singleMaxReads; + } + } + else + { + if (cancellationTokenSource?.IsCancellationRequested == true) return; + foreach (var item in this.Clone().ToPageList(pageIndex, pageSize)) + { + if (cancellationTokenSource?.IsCancellationRequested == true) return; + action.Invoke(item); + } + } + } + totalNumber = count; + } public List ToOffsetPage(int pageIndex, int pageSize) { diff --git a/Src/Asp.Net/SqlSugar/Interface/IQueryable.cs b/Src/Asp.Net/SqlSugar/Interface/IQueryable.cs index 4b6a5ed6e..d88c863a6 100644 --- a/Src/Asp.Net/SqlSugar/Interface/IQueryable.cs +++ b/Src/Asp.Net/SqlSugar/Interface/IQueryable.cs @@ -111,6 +111,7 @@ namespace SqlSugar void ForEach(Action action, int singleMaxReads = 300, System.Threading.CancellationTokenSource cancellationTokenSource = null); Task ForEachAsync(Action action, int singleMaxReads = 300, System.Threading.CancellationTokenSource cancellationTokenSource = null); void ForEachByPage(Action action, int pageIndex, int pageSize, ref int totalNumber, int singleMaxReads = 300, System.Threading.CancellationTokenSource cancellationTokenSource = null); + Task ForEachByPageAsync(Action action, int pageIndex, int pageSize, RefAsync totalNumber, int singleMaxReads = 300, System.Threading.CancellationTokenSource cancellationTokenSource = null); int Count(); Task CountAsync(); int Count(Expression> expression);