From 35d0b64aed40cd6ec82caed2b8bbb4faae0aa498 Mon Sep 17 00:00:00 2001 From: sunkaixuan <610262374@qq.com> Date: Sun, 18 Dec 2022 14:46:27 +0800 Subject: [PATCH] Update Subquery with MergeTable() --- .../QueryableProvider/QueryableProvider.cs | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/Src/Asp.Net/SqlSugar/Abstract/QueryableProvider/QueryableProvider.cs b/Src/Asp.Net/SqlSugar/Abstract/QueryableProvider/QueryableProvider.cs index 851783428..ed8420e89 100644 --- a/Src/Asp.Net/SqlSugar/Abstract/QueryableProvider/QueryableProvider.cs +++ b/Src/Asp.Net/SqlSugar/Abstract/QueryableProvider/QueryableProvider.cs @@ -1100,6 +1100,10 @@ namespace SqlSugar public virtual ISugarQueryable MergeTable() { + if (this.QueryBuilder.SubToListParameters != null && this.QueryBuilder.SubToListParameters.Count > 0) + { + return MergeTableWithSubToList(); + } Check.Exception(this.MapperAction != null || this.MapperActionWithCache != null,ErrorMessage.GetThrowMessage( "'Mapper’ needs to be written after ‘MergeTable’ ", "Mapper 只能在 MergeTable 之后使用")); //Check.Exception(this.QueryBuilder.SelectValue.IsNullOrEmpty(),ErrorMessage.GetThrowMessage( "MergeTable need to use Queryable.Select Method .", "使用MergeTable之前必须要有Queryable.Select方法")); //Check.Exception(this.QueryBuilder.Skip > 0 || this.QueryBuilder.Take > 0 || this.QueryBuilder.OrderByValue.HasValue(),ErrorMessage.GetThrowMessage( "MergeTable Queryable cannot Take Skip OrderBy PageToList ", "使用 MergeTable不能有 Take Skip OrderBy PageToList 等操作,你可以在Mergetable之后操作")); @@ -1116,6 +1120,33 @@ namespace SqlSugar return result; } + public virtual ISugarQueryable MergeTableWithSubToList() + { + _ToSql(); + var clone = this.Clone(); + + clone.QueryBuilder.AppendValues = null; + clone.QueryBuilder.SubToListParameters = null; + clone.QueryBuilder.AppendColumns = null; + Check.Exception(this.MapperAction != null || this.MapperActionWithCache != null, ErrorMessage.GetThrowMessage("'Mapper’ needs to be written after ‘MergeTable’ ", "Mapper 只能在 MergeTable 之后使用")); + //Check.Exception(this.QueryBuilder.SelectValue.IsNullOrEmpty(),ErrorMessage.GetThrowMessage( "MergeTable need to use Queryable.Select Method .", "使用MergeTable之前必须要有Queryable.Select方法")); + //Check.Exception(this.QueryBuilder.Skip > 0 || this.QueryBuilder.Take > 0 || this.QueryBuilder.OrderByValue.HasValue(),ErrorMessage.GetThrowMessage( "MergeTable Queryable cannot Take Skip OrderBy PageToList ", "使用 MergeTable不能有 Take Skip OrderBy PageToList 等操作,你可以在Mergetable之后操作")); + var sqlobj = clone.ToSql(); + var index = QueryBuilder.WhereIndex + 1; + var result = this.Context.Queryable().AS(SqlBuilder.GetPackTable(sqlobj.Key, "MergeTable")).AddParameters(sqlobj.Value).Select("*").With(SqlWith.Null); + result.QueryBuilder.WhereIndex = index; + result.QueryBuilder.LambdaExpressions.ParameterIndex = QueryBuilder.LambdaExpressions.ParameterIndex++; + result.QueryBuilder.LambdaExpressions.Index = QueryBuilder.LambdaExpressions.Index++; + if (this.Context.CurrentConnectionConfig.DbType == DbType.Oracle) + { + result.Select("MergeTable.*"); + } + result.QueryBuilder.AppendValues = this.QueryBuilder.AppendValues; + result.QueryBuilder.SubToListParameters = this.QueryBuilder.SubToListParameters; + result.QueryBuilder.AppendColumns = this.QueryBuilder.AppendColumns; + return result; + } + public ISugarQueryable SplitTable(DateTime beginTime, DateTime endTime) { var splitColumn = this.EntityInfo.Columns.FirstOrDefault(it => it.PropertyInfo.GetCustomAttribute() != null);