Update queryable.ToDictionary

This commit is contained in:
sunkaixuan 2023-05-11 12:36:08 +08:00
parent 3574a54666
commit 43df2fa762
3 changed files with 32 additions and 4 deletions

View File

@ -410,8 +410,17 @@ namespace SqlSugar
keyName = this.QueryBuilder.TableShortName + "." + keyName;
valueName = this.QueryBuilder.TableShortName + "." + valueName;
}
var result = this.Select<KeyValuePair<string, object>>(keyName + "," + valueName).ToList().ToDictionary(it => it.Key.ObjToString(), it => it.Value);
return result;
var isJson=this.Context.EntityMaintenance.GetEntityInfo<T>().Columns.Where(it => it.IsJson && it.PropertyName == ExpressionTool.GetMemberName(value)).Any();
if (isJson)
{
var result = this.Select<T>(keyName + "," + valueName).ToList().ToDictionary(ExpressionTool.GetMemberName(key), ExpressionTool.GetMemberName(value));
return result;
}
else
{
var result = this.Select<KeyValuePair<string, object>>(keyName + "," + valueName).ToList().ToDictionary(it => it.Key.ObjToString(), it => it.Value);
return result;
}
}
public List<Dictionary<string, object>> ToDictionaryList()

View File

@ -559,8 +559,17 @@ ParameterT parameter)
var keyName = QueryBuilder.GetExpressionValue(key, ResolveExpressType.FieldSingle).GetResultString();
var valueName = QueryBuilder.GetExpressionValue(value, ResolveExpressType.FieldSingle).GetResultString();
var list = await this.Select<KeyValuePair<string, object>>(keyName + "," + valueName).ToListAsync();
var result = list.ToDictionary(it => it.Key.ObjToString(), it => it.Value);
return result;
var isJson = this.Context.EntityMaintenance.GetEntityInfo<T>().Columns.Where(it => it.IsJson && it.PropertyName == ExpressionTool.GetMemberName(value)).Any();
if (isJson)
{
var result = this.Select<T>(keyName + "," + valueName).ToList().ToDictionary(ExpressionTool.GetMemberName(key), ExpressionTool.GetMemberName(value));
return result;
}
else
{
var result = list.ToDictionary(it => it.Key.ObjToString(), it => it.Value);
return result;
}
}
public async Task<List<T>> ToTreeAsync(Expression<Func<T, IEnumerable<object>>> childListExpression, Expression<Func<T, object>> parentIdExpression, object rootValue, object[] childIds)
{

View File

@ -11,6 +11,16 @@ namespace SqlSugar
{
public static class CommonExtensions
{
public static Dictionary<string, object> ToDictionary<T>(this List<T> list, string keyPropertyName, string valuePropertyName)
{
var keyProperty = typeof(T).GetProperty(keyPropertyName);
var valueProperty = typeof(T).GetProperty(valuePropertyName);
return list.ToDictionary(
item => keyProperty.GetValue(item)+"",
item => valueProperty.GetValue(item)
);
}
public static MethodInfo GetMyMethod(this Type type,string name, int argCount)
{
return type.GetMethods().FirstOrDefault(it => it.Name == name && it.GetParameters().Length == argCount);