Add ForEachByPageAsync

This commit is contained in:
sunkaixuan 2022-04-13 01:07:43 +08:00
parent 80ededd501
commit 9897a0e5c5
2 changed files with 38 additions and 0 deletions

View File

@ -1477,6 +1477,43 @@ namespace SqlSugar
}
totalNumber = count;
}
public virtual async Task ForEachByPageAsync(Action<T> action, int pageIndex, int pageSize,RefAsync<int> 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<T> ToOffsetPage(int pageIndex, int pageSize)
{

View File

@ -111,6 +111,7 @@ namespace SqlSugar
void ForEach(Action<T> action, int singleMaxReads = 300, System.Threading.CancellationTokenSource cancellationTokenSource = null);
Task ForEachAsync(Action<T> action, int singleMaxReads = 300, System.Threading.CancellationTokenSource cancellationTokenSource = null);
void ForEachByPage(Action<T> action, int pageIndex, int pageSize, ref int totalNumber, int singleMaxReads = 300, System.Threading.CancellationTokenSource cancellationTokenSource = null);
Task ForEachByPageAsync(Action<T> action, int pageIndex, int pageSize, RefAsync<int> totalNumber, int singleMaxReads = 300, System.Threading.CancellationTokenSource cancellationTokenSource = null);
int Count();
Task<int> CountAsync();
int Count(Expression<Func<T, bool>> expression);