Add ForEachAsync

This commit is contained in:
sunkaixuan 2022-04-13 01:03:43 +08:00
parent 293780246f
commit 80ededd501
4 changed files with 39 additions and 5 deletions

View File

@ -91,10 +91,16 @@ namespace OrmTest
db.Insertable(new Tree1() { Id = 4, Name = "02" }).ExecuteCommand();
db.Insertable(new Tree1() { Id = 5, Name = "0201", ParentId = 2 }).ExecuteCommand();
db.Insertable(new Tree1() { Id = 6, Name = "020101", ParentId = 5 }).ExecuteCommand();
var list4=db.Queryable<Tree1>()
.Includes(it => it.Child,it=>it.Child,it=>it.Child)
.Includes(it => it.Parent,it=>it.Parent, it => it.Parent, it => it.Parent)
.ToList();
var list21111 = new List<Tree1>();
db.Queryable<Tree1>()
.Includes(it => it.Child)
.Includes(it => it.Parent)
.ForEach(item => {
list21111.Add(item);
}, 2);
//var json = db.Utilities.SerializeObject(list4);
db.CodeFirst.InitTables<UnitA001, UnitA002>();

View File

@ -1420,6 +1420,26 @@ namespace SqlSugar
}
}
}
public virtual async Task ForEachAsync(Action<T> action, int singleMaxReads = 300, System.Threading.CancellationTokenSource cancellationTokenSource = null)
{
Check.Exception(this.QueryBuilder.Skip > 0 || this.QueryBuilder.Take > 0, ErrorMessage.GetThrowMessage("no support Skip take, use PageForEach", "不支持Skip Take,请使用 Queryale.PageForEach"));
RefAsync<int> totalNumber = 0;
RefAsync<int> totalPage = 1;
for (int i = 1; i <= totalPage; i++)
{
if (cancellationTokenSource?.IsCancellationRequested == true) return;
var queryable = this.Clone();
var page =
totalPage == 1 ?
await queryable.ToPageListAsync(i, singleMaxReads, totalNumber, totalPage) :
await queryable.ToPageListAsync(i, singleMaxReads);
foreach (var item in page)
{
if (cancellationTokenSource?.IsCancellationRequested == true) return;
action.Invoke(item);
}
}
}
public virtual void ForEachByPage(Action<T> action, int pageIndex, int pageSize, ref int totalNumber, int singleMaxReads = 300, System.Threading.CancellationTokenSource cancellationTokenSource = null)
{
int count = this.Clone().Count();
@ -1769,6 +1789,12 @@ namespace SqlSugar
this.Context.MappingTables = oldMapping;
return await this.Clone().ToPageListAsync(pageIndex, pageSize);
}
public Task<List<T>> ToPageListAsync(int pageNumber, int pageSize, RefAsync<int> totalNumber, RefAsync<int> totalPage)
{
var result = ToPageListAsync(pageNumber, pageSize, totalNumber);
totalPage = (totalNumber + pageSize - 1) / pageSize;
return result;
}
public async Task<string> ToJsonAsync()
{
if (IsCache)

View File

@ -109,6 +109,7 @@ namespace SqlSugar
ISugarQueryable<T> Select(string select);
ISugarQueryable<T> MergeTable();
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);
int Count();
Task<int> CountAsync();
@ -173,6 +174,7 @@ namespace SqlSugar
List<T> ToPageList(int pageNumber, int pageSize, ref int totalNumber);
List<T> ToPageList(int pageNumber, int pageSize, ref int totalNumber,ref int totalPage);
Task<List<T>> ToPageListAsync(int pageNumber, int pageSize, RefAsync<int> totalNumber);
Task<List<T>> ToPageListAsync(int pageNumber, int pageSize, RefAsync<int> totalNumber, RefAsync<int> totalPage);
ISugarQueryable<T> WithCache(string cacheKey,int cacheDurationInSeconds = int.MaxValue);
ISugarQueryable<T> WithCache(int cacheDurationInSeconds = int.MaxValue);
ISugarQueryable<T> WithCacheIF(bool isCache, int cacheDurationInSeconds = int.MaxValue);

View File

@ -2,7 +2,7 @@
<package >
<metadata>
<id>SqlSugarCore</id>
<version>5.0.6.8-preview02</version>
<version>5.0.6.8-preview03</version>
<authors>sunkaixuan</authors>
<owners>果糖大数据科技</owners>
<licenseUrl>http://www.apache.org/licenses/LICENSE-2.0.html</licenseUrl>