Performance optimization

This commit is contained in:
sunkaixuan
2023-04-03 18:16:05 +08:00
parent 2f284a20c3
commit f86b1ccd3f
2 changed files with 41 additions and 26 deletions

View File

@@ -353,6 +353,40 @@ namespace SqlSugar
#endregion #endregion
#region Private methods #region Private methods
private void _ThenMapper<T>(IEnumerable<T> list, Action<T> action)
{
MapperContext<T> result = new MapperContext<T>();
result.context = this.Context;
if (result.context.TempItems == null)
{
result.context.TempItems = new Dictionary<string, object>();
}
var key = "Queryable_To_Context";
result.context.TempItems.Add(key, result);
result.list = list.ToList();
foreach (var item in list)
{
action.Invoke(item);
}
result.context.TempItems.Remove(key);
}
private async Task _ThenMapperAsync<T>(IEnumerable<T> list, Func<T, Task> action)
{
MapperContext<T> result = new MapperContext<T>();
result.context = this.Context;
if (result.context.TempItems == null)
{
result.context.TempItems = new Dictionary<string, object>();
}
var key = "Queryable_To_Context";
result.context.TempItems.Add(key, result);
result.list = list.ToList();
foreach (var item in list)
{
await action.Invoke(item);
}
result.context.TempItems.Remove(key);
}
internal string GetN() internal string GetN()
{ {
var N = "N"; var N = "N";

View File

@@ -1665,37 +1665,18 @@ namespace SqlSugar
} }
public void ThenMapper<T>(IEnumerable<T> list, Action<T> action) public void ThenMapper<T>(IEnumerable<T> list, Action<T> action)
{ {
MapperContext<T> result = new MapperContext<T>(); this.Context.Utilities.PageEach(list, 200, pageList =>
result.context = this.Context;
if (result.context.TempItems == null)
{ {
result.context.TempItems = new Dictionary<string, object>(); _ThenMapper(pageList, action);
} });
var key = "Queryable_To_Context";
result.context.TempItems.Add(key, result);
result.list = list.ToList();
foreach (var item in list)
{
action.Invoke(item);
}
result.context.TempItems.Remove(key);
} }
public async Task ThenMapperAsync<T>(IEnumerable<T> list, Func<T, Task> action) public async Task ThenMapperAsync<T>(IEnumerable<T> list, Func<T, Task> action)
{ {
MapperContext<T> result = new MapperContext<T>(); await this.Context.Utilities.PageEachAsync(list, 200,async pageList =>
result.context = this.Context;
if (result.context.TempItems == null)
{ {
result.context.TempItems = new Dictionary<string, object>(); await _ThenMapperAsync(pageList, action);
} });
var key = "Queryable_To_Context";
result.context.TempItems.Add(key, result);
result.list = list.ToList();
foreach (var item in list)
{
await action.Invoke(item);
}
result.context.TempItems.Remove(key);
} }
#endregion #endregion
} }