db.Queryable.Includes.Select.ToList

This commit is contained in:
sunkaixuan 2023-05-03 23:29:08 +08:00
parent 6b6839bf60
commit aa88be7e93
5 changed files with 48 additions and 4 deletions

View File

@ -149,6 +149,17 @@ namespace SqlSugar
}
QueryBuilder.AppendValues.Add(addItems);
}
if (QueryBuilder?.AppendNavInfo != null)
{
var navResult = new AppendNavResult();
foreach (var item in QueryBuilder?.AppendNavInfo.AppendProperties)
{
var vi = dataReader.GetOrdinal(item.Key);
var value = dataReader.GetValue(vi);
navResult.result.Add("SugarNav_"+item.Key,value);
}
QueryBuilder?.AppendNavInfo.Result.Add(navResult);
}
}
private List<string> GetDataReaderNames(IDataReader dataReader,ref string types)

View File

@ -7,8 +7,13 @@ namespace SqlSugar
{
internal class AppendNavInfo
{
public Dictionary<string, MappingNavColumnInfo> MappingNavProperties = new Dictionary<string, MappingNavColumnInfo>();
public Dictionary<string, string> AppendProperties = new Dictionary<string, string>();
public Dictionary<string, MappingNavColumnInfo> MappingNavProperties { get; set; }= new Dictionary<string, MappingNavColumnInfo>();
public Dictionary<string, string> AppendProperties { get; set; }= new Dictionary<string, string>();
public List<AppendNavResult> Result { get; set; } =new List<AppendNavResult>();
}
internal class AppendNavResult
{
public Dictionary<string, object> result = new Dictionary<string, object>();
}
internal class MappingNavColumnInfo
{

View File

@ -638,12 +638,24 @@ namespace SqlSugar
var manager =it;
var p= it.GetType().GetProperty("RootList");
var tType = it.GetType().GenericTypeArguments[0];
var columns = this.Context.EntityMaintenance.GetEntityInfo(tType).Columns;
var listType = typeof(List<>).MakeGenericType(tType);
var outList = Activator.CreateInstance(listType);
p.SetValue(it, outList);
var index = 0;
foreach (var item in result)
{
(outList as IList).Add(Activator.CreateInstance(tType));
var addItem = Activator.CreateInstance(tType);
var appendResult=this.QueryBuilder.AppendNavInfo.Result[index];
foreach (var kv in appendResult.result)
{
var propertyName=kv.Key.Replace("SugarNav_", "");
var propertyInfo = columns.First(i => i.PropertyName == propertyName).PropertyInfo;
propertyInfo.SetValue(addItem, kv.Value);
}
(outList as IList).Add(addItem);
index++;
}
it.GetType().GetMethod("Execute").Invoke(it,null);
}
@ -1657,7 +1669,14 @@ namespace SqlSugar
asyncQueryableBuilder.AppendColumns = this.Context.Utilities.TranslateCopy(this.QueryBuilder.AppendColumns);
asyncQueryableBuilder.AppendValues = this.Context.Utilities.TranslateCopy(this.QueryBuilder.AppendValues);
asyncQueryableBuilder.RemoveFilters = this.QueryBuilder.RemoveFilters?.ToArray();
asyncQueryableBuilder.AppendNavInfo = this.Context.Utilities.TranslateCopy(this.QueryBuilder.AppendNavInfo);
if (this.QueryBuilder.AppendNavInfo != null)
{
asyncQueryableBuilder.AppendNavInfo = new AppendNavInfo()
{
AppendProperties= this.QueryBuilder.AppendNavInfo.AppendProperties.ToDictionary(it => it.Key, it => it.Value),
MappingNavProperties= this.QueryBuilder.AppendNavInfo.MappingNavProperties.ToDictionary(it=>it.Key,it=>it.Value)
} ;
}
}
private static JoinQueryInfo CopyJoinInfo(JoinQueryInfo it)

View File

@ -806,6 +806,11 @@ namespace SqlSugar
{
return "*";
}
if (this.AppendNavInfo?.AppendProperties?.Any() ==true)
{
result += ",";
result += string.Join(",",this.AppendNavInfo.AppendProperties.Select(it=>it.Value+ " AS SugarNav_" + it.Key));
}
if (result.Contains("/**/*"))
{
return result.Replace("/**/*", "");

View File

@ -311,6 +311,10 @@ namespace SqlSugar
}
MemberAssignment memberAssignment = (MemberAssignment)binding;
var memberName = memberAssignment.Member.Name;
if (this.Context?.SugarContext?.QueryBuilder?.AppendNavInfo?.MappingNavProperties?.ContainsKey(memberName) == true)
{
continue;
}
var item = memberAssignment.Expression;
if (item.Type.IsClass()&& item is MemberExpression &&(item as MemberExpression).Expression is ParameterExpression)
{