Add Queryable.ToTree(string,string,object,string)

This commit is contained in:
sunkaixuan
2023-11-12 14:20:03 +08:00
parent 95ebafa3ed
commit f1ed2c71e8
4 changed files with 34 additions and 1 deletions

View File

@@ -340,6 +340,16 @@ namespace SqlSugar
}
return result;
}
public List<T> ToTree(string childPropertyName, string parentIdPropertyName, object rootValue, string primaryKeyPropertyName)
{
var entity = this.Context.EntityMaintenance.GetEntityInfo<T>();
var pk = primaryKeyPropertyName;
var list = this.ToList();
Expression<Func<T,IEnumerable<object> >> childListExpression = (Expression<Func<T, IEnumerable<object>>>)ExpressionBuilderHelper.CreateExpressionSelectField(typeof(T),childPropertyName,typeof(IEnumerable<object>));
Expression<Func<T, object>> parentIdExpression = (Expression<Func<T, object>>)ExpressionBuilderHelper.CreateExpressionSelectFieldObject(typeof(T), parentIdPropertyName);
return GetTreeRoot(childListExpression, parentIdExpression, pk, list, rootValue) ?? new List<T>();
}
public List<T> ToTree(Expression<Func<T, IEnumerable<object>>> childListExpression, Expression<Func<T, object>> parentIdExpression, object rootValue, Expression<Func<T, object>> primaryKeyExpression)
{
var entity = this.Context.EntityMaintenance.GetEntityInfo<T>();

View File

@@ -580,6 +580,15 @@ ParameterT parameter)
return result;
}
}
public async Task<List<T>> ToTreeAsync(string childPropertyName, string parentIdPropertyName, object rootValue, string primaryKeyPropertyName)
{
var entity = this.Context.EntityMaintenance.GetEntityInfo<T>();
var pk = primaryKeyPropertyName;
var list =await this.ToListAsync();
Expression<Func<T, IEnumerable<object>>> childListExpression = (Expression<Func<T, IEnumerable<object>>>)ExpressionBuilderHelper.CreateExpressionSelectField(typeof(T), childPropertyName, typeof(IEnumerable<object>));
Expression<Func<T, object>> parentIdExpression = (Expression<Func<T, object>>)ExpressionBuilderHelper.CreateExpressionSelectFieldObject(typeof(T), parentIdPropertyName);
return GetTreeRoot(childListExpression, parentIdExpression, pk, list, rootValue) ?? new List<T>();
}
public async Task<List<T>> ToTreeAsync(Expression<Func<T, IEnumerable<object>>> childListExpression, Expression<Func<T, object>> parentIdExpression, object rootValue, object[] childIds)
{
var list = await this.ToListAsync();

View File

@@ -240,9 +240,10 @@ namespace SqlSugar
Task<List<T>> ToParentListAsync(Expression<Func<T, object>> parentIdExpression, object primaryKeyValue);
Task<List<T>> ToParentListAsync(Expression<Func<T, object>> parentIdExpression, object primaryKeyValue, Expression<Func<T, bool>> parentWhereExpression);
List<T> ToTree(string childPropertyName, string parentIdPropertyName, object rootValue, string primaryKeyPropertyName);
List<T> ToTree(Expression<Func<T,IEnumerable<object>>> childListExpression, Expression<Func<T,object>> parentIdExpression,object rootValue);
List<T> ToTree(Expression<Func<T, IEnumerable<object>>> childListExpression, Expression<Func<T, object>> parentIdExpression, object rootValue, Expression<Func<T, object>> primaryKeyExpression);
Task<List<T>> ToTreeAsync(string childPropertyName, string parentIdPropertyName, object rootValue, string primaryKeyPropertyName);
Task<List<T>> ToTreeAsync(Expression<Func<T, IEnumerable<object>>> childListExpression, Expression<Func<T, object>> parentIdExpression, object rootValue);
Task<List<T>> ToTreeAsync(Expression<Func<T, IEnumerable<object>>> childListExpression, Expression<Func<T, object>> parentIdExpression, object rootValue, Expression<Func<T, object>> primaryKeyExpression);
List<T> ToTree(Expression<Func<T, IEnumerable<object>>> childListExpression, Expression<Func<T, object>> parentIdExpression, object rootValue, object[] childIds);

View File

@@ -92,6 +92,19 @@ namespace SqlSugar
LambdaExpression lambda = Expression.Lambda(funcType, property, parameter);
return lambda;
}
public static Expression CreateExpressionSelectFieldObject(Type classType, string propertyName)
{
ParameterExpression parameter = Expression.Parameter(classType, "it");
PropertyInfo propertyInfo = classType.GetProperty(propertyName);
MemberExpression property = Expression.Property(parameter, propertyInfo);
UnaryExpression convert = Expression.Convert(property, typeof(object));
var funcType = typeof(Func<,>).MakeGenericType(classType, typeof(object));
LambdaExpression lambda = Expression.Lambda(funcType, convert, parameter);
return lambda;
}
}
internal static class LinqRuntimeTypeBuilder
{