Browse Source

Update Core

pull/15/head
sunkaixuan 3 years ago
parent
commit
dbe9092b3a
  1. 17
      Src/Asp.NetCore2/SqlSugar/Abstract/QueryableProvider/QueryableContext.cs
  2. 73
      Src/Asp.NetCore2/SqlSugar/Abstract/QueryableProvider/QueryableProvider.cs
  3. 37
      Src/Asp.NetCore2/SqlSugar/Abstract/SugarProvider/SqlSugarProvider.cs
  4. 3
      Src/Asp.NetCore2/SqlSugar/Interface/IQueryable.cs
  5. 5
      Src/Asp.NetCore2/SqlSugar/Interface/ISqlSugarClient.cs
  6. 13
      Src/Asp.NetCore2/SqlSugar/SqlSugarClient.cs
  7. 10
      Src/Asp.NetCore2/SqlSugar/SqlSugarScope.cs

17
Src/Asp.NetCore2/SqlSugar/Abstract/QueryableProvider/QueryableContext.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; }
}
}

73
Src/Asp.NetCore2/SqlSugar/Abstract/QueryableProvider/QueryableProvider.cs

@ -637,7 +637,14 @@ namespace SqlSugar
{
if (item != null)
{
values.Add(item.ToString().ToSqlValue());
if (UtilMethods.IsNumber(item.GetType().Name))
{
values.Add(item.ToString());
}
else
{
values.Add(item.ToString().ToSqlValue());
}
}
}
this.Where(string.Format(QueryBuilder.InTemplate, filed, string.Join(",", values)));
@ -1300,6 +1307,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"));
@ -2299,7 +2367,8 @@ namespace SqlSugar
{
FieldName=this.SqlBuilder.GetTranslationColumnName(whereCol.DbColumnName),
ConditionalType= ConditionalType.In,
FieldValue=string.Join(",",inValues.Distinct())
FieldValue=string.Join(",",inValues.Distinct()),
CSharpTypeName=whereCol.PropertyInfo.PropertyType.Name
}
};
var list = this.Context.Queryable<TObject>().Where(wheres).ToList();

37
Src/Asp.NetCore2/SqlSugar/Abstract/SugarProvider/SqlSugarProvider.cs

@ -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
}
}

3
Src/Asp.NetCore2/SqlSugar/Interface/IQueryable.cs

@ -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();

5
Src/Asp.NetCore2/SqlSugar/Interface/ISqlSugarClient.cs

@ -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
}
}

13
Src/Asp.NetCore2/SqlSugar/SqlSugarClient.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;

10
Src/Asp.NetCore2/SqlSugar/SqlSugarScope.cs

@ -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…
Cancel
Save