mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-09-20 02:29:39 +08:00
Add Queryable.ToChildList
This commit is contained in:
@@ -926,6 +926,22 @@ namespace SqlSugar
|
|||||||
var list= this.ToPivotList(columnSelector, rowSelector, dataSelector);
|
var list= this.ToPivotList(columnSelector, rowSelector, dataSelector);
|
||||||
return this.Context.Utilities.SerializeObject(list);
|
return this.Context.Utilities.SerializeObject(list);
|
||||||
}
|
}
|
||||||
|
public List<T> ToChildList(Expression<Func<T, object>> parentIdExpression, object primaryKeyValue)
|
||||||
|
{
|
||||||
|
var entity = this.Context.EntityMaintenance.GetEntityInfo<T>();
|
||||||
|
Check.Exception(entity.Columns.Where(it => it.IsPrimarykey).Count() == 0, "No Primary key");
|
||||||
|
var pk = entity.Columns.Where(it => it.IsPrimarykey).First().PropertyName;
|
||||||
|
var list = this.ToList();
|
||||||
|
return GetChildList(parentIdExpression, pk, list, primaryKeyValue);
|
||||||
|
}
|
||||||
|
public async Task<List<T>> ToChildListAsync(Expression<Func<T, object>> parentIdExpression, object primaryKeyValue)
|
||||||
|
{
|
||||||
|
var entity = this.Context.EntityMaintenance.GetEntityInfo<T>();
|
||||||
|
Check.Exception(entity.Columns.Where(it => it.IsPrimarykey).Count() == 0, "No Primary key");
|
||||||
|
var pk = entity.Columns.Where(it => it.IsPrimarykey).First().PropertyName;
|
||||||
|
var list = await this.ToListAsync();
|
||||||
|
return GetChildList(parentIdExpression,pk,list, primaryKeyValue);
|
||||||
|
}
|
||||||
public List<T> ToParentList(Expression<Func<T, object>> parentIdExpression, object primaryKeyValue)
|
public List<T> ToParentList(Expression<Func<T, object>> parentIdExpression, object primaryKeyValue)
|
||||||
{
|
{
|
||||||
List<T> result = new List<T>() { };
|
List<T> result = new List<T>() { };
|
||||||
@@ -1440,6 +1456,41 @@ namespace SqlSugar
|
|||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region Private Methods
|
#region Private Methods
|
||||||
|
private List<T> GetChildList(Expression<Func<T, object>> parentIdExpression, string pkName, List<T> list, object rootValue,bool isRoot=true)
|
||||||
|
{
|
||||||
|
var exp = (parentIdExpression as LambdaExpression).Body;
|
||||||
|
if (exp is UnaryExpression)
|
||||||
|
{
|
||||||
|
exp = (exp as UnaryExpression).Operand;
|
||||||
|
}
|
||||||
|
var parentIdName = (exp as MemberExpression).Member.Name;
|
||||||
|
List<T> result = list.Where(it =>
|
||||||
|
{
|
||||||
|
var parentValue = it.GetType().GetProperty(parentIdName).GetValue(it);
|
||||||
|
return parentValue.ObjToString() == rootValue.ObjToString();
|
||||||
|
|
||||||
|
}).ToList();
|
||||||
|
if (result != null && result.Count > 0)
|
||||||
|
{
|
||||||
|
List<T> childList = new List<T>();
|
||||||
|
foreach (var item in result)
|
||||||
|
{
|
||||||
|
var pkValue = item.GetType().GetProperty(pkName).GetValue(item);
|
||||||
|
childList.AddRange(GetChildList(parentIdExpression, pkName, list, pkValue,false));
|
||||||
|
}
|
||||||
|
result.AddRange(childList);
|
||||||
|
}
|
||||||
|
if (isRoot)
|
||||||
|
{
|
||||||
|
result.AddRange(list.Where(it =>
|
||||||
|
{
|
||||||
|
var pkValue = it.GetType().GetProperty(pkName).GetValue(it);
|
||||||
|
return pkValue.ObjToString() == rootValue.ObjToString();
|
||||||
|
|
||||||
|
}).ToList());
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
private List<T> GetTreeRoot(Expression<Func<T, IEnumerable<object>>> childListExpression, Expression<Func<T, object>> parentIdExpression, string pk, List<T> list,object rootValue)
|
private List<T> GetTreeRoot(Expression<Func<T, IEnumerable<object>>> childListExpression, Expression<Func<T, object>> parentIdExpression, string pk, List<T> list,object rootValue)
|
||||||
{
|
{
|
||||||
var childName = ((childListExpression as LambdaExpression).Body as MemberExpression).Member.Name;
|
var childName = ((childListExpression as LambdaExpression).Body as MemberExpression).Member.Name;
|
||||||
|
@@ -143,6 +143,8 @@ namespace SqlSugar
|
|||||||
string ToJsonPage(int pageIndex, int pageSize, ref int totalNumber);
|
string ToJsonPage(int pageIndex, int pageSize, ref int totalNumber);
|
||||||
Task<string> ToJsonPageAsync(int pageIndex, int pageSize, RefAsync<int> totalNumber);
|
Task<string> ToJsonPageAsync(int pageIndex, int pageSize, RefAsync<int> totalNumber);
|
||||||
KeyValuePair<string, List<SugarParameter>> ToSql();
|
KeyValuePair<string, List<SugarParameter>> ToSql();
|
||||||
|
List<T> ToChildList(Expression<Func<T, object>> parentIdExpression, object primaryKeyValue);
|
||||||
|
Task<List<T>> ToChildListAsync(Expression<Func<T, object>> parentIdExpression, object primaryKeyValue);
|
||||||
List<T> ToParentList(Expression<Func<T, object>> parentIdExpression, object primaryKeyValue);
|
List<T> ToParentList(Expression<Func<T, object>> parentIdExpression, object primaryKeyValue);
|
||||||
Task<List<T>> ToParentListAsync(Expression<Func<T, object>> parentIdExpression, object primaryKeyValue);
|
Task<List<T>> ToParentListAsync(Expression<Func<T, object>> parentIdExpression, object primaryKeyValue);
|
||||||
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);
|
||||||
|
Reference in New Issue
Block a user