mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-08-23 22:11:36 +08:00
Add ThenMapper
This commit is contained in:
parent
3b64a53cf2
commit
eb85b980ee
@ -100,6 +100,7 @@
|
||||
<Compile Include="UnitTest\Models\UserEntity.cs" />
|
||||
<Compile Include="UnitTest\Models\UserRoleEntity.cs" />
|
||||
<Compile Include="UnitTest\UCustom01.cs" />
|
||||
<Compile Include="UnitTest\UCustom012.cs" />
|
||||
<Compile Include="UnitTest\UCustom09.cs" />
|
||||
<Compile Include="UnitTest\UCustom011.cs" />
|
||||
<Compile Include="UnitTest\UCustom02.cs" />
|
||||
|
@ -0,0 +1,17 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SqlSugar
|
||||
{
|
||||
public class MapperContext<T>
|
||||
{
|
||||
public ISqlSugarClient context { get; set; }
|
||||
|
||||
public List<T> list { get; set; }
|
||||
public Dictionary<string,object> TempChildLists { get; set; }
|
||||
}
|
||||
}
|
@ -1300,6 +1300,67 @@ namespace SqlSugar
|
||||
InitMapping();
|
||||
return _ToList<T>();
|
||||
}
|
||||
public List<T> SetContext<ParameterT>(Expression<Func<T, object>> thisFiled, Expression<Func<object>> mappingFiled, ParameterT parameter)
|
||||
{
|
||||
List<T> result = new List<T>();
|
||||
var entity = this.Context.EntityMaintenance.GetEntityInfo<ParameterT>();
|
||||
var queryableContext = this.Context.TempItems["Queryable_To_Context"] as MapperContext<ParameterT>;
|
||||
var list = queryableContext.list;
|
||||
var pkName = (((mappingFiled as LambdaExpression).Body as UnaryExpression).Operand as MemberExpression).Member.Name;
|
||||
var key = thisFiled.ToString() +typeof(ParameterT).FullName + typeof(T).FullName;
|
||||
var ids = list.Select(it => it.GetType().GetProperty(pkName).GetValue(it)).ToArray();
|
||||
if (queryableContext.TempChildLists == null)
|
||||
queryableContext.TempChildLists = new Dictionary<string, object>();
|
||||
if (list != null && queryableContext.TempChildLists.ContainsKey(key))
|
||||
{
|
||||
result = (List<T>)queryableContext.TempChildLists[key];
|
||||
}
|
||||
else
|
||||
{
|
||||
if (queryableContext.TempChildLists == null)
|
||||
queryableContext.TempChildLists = new Dictionary<string, object>();
|
||||
this.Context.Utilities.PageEach(ids, 200, pageIds =>
|
||||
{
|
||||
result.AddRange(this.Clone().In(thisFiled, pageIds).ToList());
|
||||
});
|
||||
queryableContext.TempChildLists[key]= result;
|
||||
}
|
||||
var name = (((thisFiled as LambdaExpression).Body as UnaryExpression).Operand as MemberExpression).Member.Name;
|
||||
var pkValue = parameter.GetType().GetProperty(pkName).GetValue(parameter);
|
||||
result = result.Where(it => it.GetType().GetProperty(name).GetValue(it).ObjToString() == pkValue.ObjToString()).ToList();
|
||||
return result;
|
||||
}
|
||||
|
||||
public async Task<List<T>> SetContextAsync<ParameterT>(Expression<Func<T, object>> thisFiled, Expression<Func<object>> mappingFiled, ParameterT parameter)
|
||||
{
|
||||
List<T> result = new List<T>();
|
||||
var entity = this.Context.EntityMaintenance.GetEntityInfo<ParameterT>();
|
||||
var queryableContext = this.Context.TempItems["Queryable_To_Context"] as MapperContext<ParameterT>;
|
||||
var list = queryableContext.list;
|
||||
var pkName = (((mappingFiled as LambdaExpression).Body as UnaryExpression).Operand as MemberExpression).Member.Name;
|
||||
var key = thisFiled.ToString() + typeof(ParameterT).FullName + typeof(T).FullName;
|
||||
var ids = list.Select(it => it.GetType().GetProperty(pkName).GetValue(it)).ToArray();
|
||||
if (queryableContext.TempChildLists == null)
|
||||
queryableContext.TempChildLists = new Dictionary<string, object>();
|
||||
if (list != null && queryableContext.TempChildLists.ContainsKey(key))
|
||||
{
|
||||
result = (List<T>)queryableContext.TempChildLists[key];
|
||||
}
|
||||
else
|
||||
{
|
||||
if (queryableContext.TempChildLists == null)
|
||||
queryableContext.TempChildLists = new Dictionary<string, object>();
|
||||
await this.Context.Utilities.PageEachAsync(ids, 200, async pageIds =>
|
||||
{
|
||||
result.AddRange(await this.Clone().In(thisFiled, pageIds).ToListAsync());
|
||||
});
|
||||
queryableContext.TempChildLists[key] = result;
|
||||
}
|
||||
var name = (((thisFiled as LambdaExpression).Body as UnaryExpression).Operand as MemberExpression).Member.Name;
|
||||
var pkValue = parameter.GetType().GetProperty(pkName).GetValue(parameter);
|
||||
result = result.Where(it => it.GetType().GetProperty(name).GetValue(it).ObjToString() == pkValue.ObjToString()).ToList();
|
||||
return result;
|
||||
}
|
||||
public virtual void ForEach(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"));
|
||||
|
@ -1271,5 +1271,42 @@ namespace SqlSugar
|
||||
return new FastestProvider<T>(this);
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region
|
||||
public 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);
|
||||
}
|
||||
public 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);
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
@ -132,7 +132,8 @@ namespace SqlSugar
|
||||
Task<TResult> AvgAsync<TResult>(Expression<Func<T, TResult>> expression);
|
||||
|
||||
List<T> ToList();
|
||||
|
||||
List<T> SetContext<ParameterT>(Expression<Func<T,object>> thisFiled, Expression<Func<object>> mappingFiled, ParameterT parameter);
|
||||
Task <List<T>> SetContextAsync<ParameterT>(Expression<Func<T, object>> thisFiled, Expression<Func<object>> mappingFiled, ParameterT parameter);
|
||||
Dictionary<string, object> ToDictionary(Expression<Func<T, object>> key, Expression<Func<T, object>> value);
|
||||
Task<Dictionary<string, object>> ToDictionaryAsync(Expression<Func<T, object>> key, Expression<Func<T, object>> value);
|
||||
List<Dictionary<string, object>> ToDictionaryList();
|
||||
|
@ -193,5 +193,10 @@ namespace SqlSugar
|
||||
IFastest<T> Fastest<T>() where T : class, new();
|
||||
#endregion
|
||||
|
||||
#region ThenMapper
|
||||
void ThenMapper<T>(IEnumerable<T> list, Action<T> action);
|
||||
Task ThenMapperAsync<T>(IEnumerable<T> list, Func<T,Task> action);
|
||||
#endregion
|
||||
|
||||
}
|
||||
}
|
@ -101,6 +101,7 @@
|
||||
<Compile Include="Entities\SugarConnection.cs" />
|
||||
<Compile Include="ExpressionsToSql\Subquery\SubTemplate.cs" />
|
||||
<Compile Include="ExpressionsToSql\Subquery\SubqueryableN.cs" />
|
||||
<Compile Include="Abstract\QueryableProvider\QueryableContext.cs" />
|
||||
<Compile Include="Infrastructure\StaticConfig.cs" />
|
||||
<Compile Include="Interface\IFastBuilder.cs" />
|
||||
<Compile Include="Interface\IFastest.cs" />
|
||||
|
@ -25,6 +25,8 @@ namespace SqlSugar
|
||||
private MappingColumnList _MappingColumns;
|
||||
private IgnoreColumnList _IgnoreColumns;
|
||||
private IgnoreColumnList _IgnoreInsertColumns;
|
||||
|
||||
|
||||
internal Guid? AsyncId { get; set; }
|
||||
internal bool? IsSingleInstance { get; set; }
|
||||
|
||||
@ -591,6 +593,17 @@ namespace SqlSugar
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region ThenMapper
|
||||
public void ThenMapper<T>(IEnumerable<T> list, Action<T> action)
|
||||
{
|
||||
this.Context.ThenMapper(list, action);
|
||||
}
|
||||
public Task ThenMapperAsync<T>(IEnumerable<T> list, Func<T, Task> action)
|
||||
{
|
||||
return this.Context.ThenMapperAsync(list,action);
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region More api
|
||||
public IContextMethods Utilities { get { return this.Context.Utilities; } set { this.Context.Utilities = value; } }
|
||||
public AopProvider Aop => this.Context.Aop;
|
||||
|
@ -662,5 +662,15 @@ namespace SqlSugar
|
||||
{
|
||||
return ScopedContext.Fastest<T>();
|
||||
}
|
||||
|
||||
public void ThenMapper<T>(IEnumerable<T> list, Action<T> action)
|
||||
{
|
||||
ScopedContext.ThenMapper(list, action);
|
||||
}
|
||||
|
||||
public Task ThenMapperAsync<T>(IEnumerable<T> list, Func<T, Task> action)
|
||||
{
|
||||
return ScopedContext.ThenMapperAsync(list, action);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user