From 1888a1394f5bdabad0a34656fb70899f6a8619f4 Mon Sep 17 00:00:00 2001 From: sunkaixuan <610262374@qq.com> Date: Fri, 8 Apr 2022 20:00:16 +0800 Subject: [PATCH] Update Queryable.ToParentList --- .../SqlServerTest/UnitTest/UCustom09.cs | 2 +- .../QueryableProvider/QueryableProvider.cs | 36 +++++++++++++++---- 2 files changed, 31 insertions(+), 7 deletions(-) diff --git a/Src/Asp.Net/SqlServerTest/UnitTest/UCustom09.cs b/Src/Asp.Net/SqlServerTest/UnitTest/UCustom09.cs index f1fddb433..93626bec3 100644 --- a/Src/Asp.Net/SqlServerTest/UnitTest/UCustom09.cs +++ b/Src/Asp.Net/SqlServerTest/UnitTest/UCustom09.cs @@ -28,7 +28,7 @@ namespace OrmTest //count = SqlFunc.AggregateSum(SqlFunc.IIF(b.客户确认状态 < 20, 1, 0)) }).ToList(); - db.Queryable().ToParentList(it => it.ParentId, "0"); + db.Queryable().LeftJoin((t,y)=>t.Id==y.Id).Where(t=>1==1).Select(t=>t).ToParentList(it => it.ParentId, "0"); } public class Unitadfaint21 diff --git a/Src/Asp.Net/SqlSugar/Abstract/QueryableProvider/QueryableProvider.cs b/Src/Asp.Net/SqlSugar/Abstract/QueryableProvider/QueryableProvider.cs index afbe48904..a87a01915 100644 --- a/Src/Asp.Net/SqlSugar/Abstract/QueryableProvider/QueryableProvider.cs +++ b/Src/Asp.Net/SqlSugar/Abstract/QueryableProvider/QueryableProvider.cs @@ -1123,16 +1123,28 @@ namespace SqlSugar var parentIdName =UtilConvert.ToMemberExpression((parentIdExpression as LambdaExpression).Body).Member.Name; var ParentInfo = entity.Columns.First(it => it.PropertyName == parentIdName); var parentPropertyName= ParentInfo.DbColumnName; - var current = this.Context.Queryable().AS(this.QueryBuilder.GetTableNameString).InSingle(primaryKeyValue); + var tableName= this.QueryBuilder.GetTableNameString; + if (this.QueryBuilder.IsSingle() == false) + { + if (this.QueryBuilder.JoinQueryInfos.Count>0) + { + tableName = this.QueryBuilder.JoinQueryInfos.First().TableName; + } + if (this.QueryBuilder.EasyJoinInfos.Count>0) + { + tableName = this.QueryBuilder.JoinQueryInfos.First().TableName; + } + } + var current = this.Context.Queryable().AS(tableName).InSingle(primaryKeyValue); if (current != null) { result.Add(current); object parentId = ParentInfo.PropertyInfo.GetValue(current,null); int i = 0; - while (parentId!=null&&this.Context.Queryable().AS(this.QueryBuilder.GetTableNameString).In(parentId).Any()) + while (parentId!=null&&this.Context.Queryable().AS(tableName).In(parentId).Any()) { Check.Exception(i > 100, ErrorMessage.GetThrowMessage("Dead cycle", "出现死循环或超出循环上限(100),检查最顶层的ParentId是否是null或者0")); - var parent = this.Context.Queryable().AS(this.QueryBuilder.GetTableNameString).InSingle(parentId); + var parent = this.Context.Queryable().AS(tableName).InSingle(parentId); result.Add(parent); parentId= ParentInfo.PropertyInfo.GetValue(parent, null); ++i; @@ -1148,16 +1160,28 @@ namespace SqlSugar var parentIdName = UtilConvert.ToMemberExpression((parentIdExpression as LambdaExpression).Body).Member.Name; var ParentInfo = entity.Columns.First(it => it.PropertyName == parentIdName); var parentPropertyName = ParentInfo.DbColumnName; - var current =await this.Context.Queryable().AS(this.QueryBuilder.GetTableNameString).InSingleAsync(primaryKeyValue); + var tableName = this.QueryBuilder.GetTableNameString; + if (this.QueryBuilder.IsSingle() == false) + { + if (this.QueryBuilder.JoinQueryInfos.Count > 0) + { + tableName = this.QueryBuilder.JoinQueryInfos.First().TableName; + } + if (this.QueryBuilder.EasyJoinInfos.Count > 0) + { + tableName = this.QueryBuilder.JoinQueryInfos.First().TableName; + } + } + var current =await this.Context.Queryable().AS(tableName).InSingleAsync(primaryKeyValue); if (current != null) { result.Add(current); object parentId = ParentInfo.PropertyInfo.GetValue(current, null); int i = 0; - while (parentId != null &&await this.Context.Queryable().AS(this.QueryBuilder.GetTableNameString).In(parentId).AnyAsync()) + while (parentId != null &&await this.Context.Queryable().AS(tableName).In(parentId).AnyAsync()) { Check.Exception(i > 100, ErrorMessage.GetThrowMessage("Dead cycle", "出现死循环或超出循环上限(100),检查最顶层的ParentId是否是null或者0")); - var parent =await this.Context.Queryable().AS(this.QueryBuilder.GetTableNameString).InSingleAsync(parentId); + var parent =await this.Context.Queryable().AS(tableName).InSingleAsync(parentId); result.Add(parent); parentId = ParentInfo.PropertyInfo.GetValue(parent, null); ++i;