Browse Source

Update Subquery with MergeTable()

pull/30/head
sunkaixuan 2 years ago
parent
commit
35d0b64aed
  1. 31
      Src/Asp.Net/SqlSugar/Abstract/QueryableProvider/QueryableProvider.cs

31
Src/Asp.Net/SqlSugar/Abstract/QueryableProvider/QueryableProvider.cs

@ -1100,6 +1100,10 @@ namespace SqlSugar
public virtual ISugarQueryable<T> 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<T> 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<T>().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<T> SplitTable(DateTime beginTime, DateTime endTime)
{
var splitColumn = this.EntityInfo.Columns.FirstOrDefault(it => it.PropertyInfo.GetCustomAttribute<SplitFieldAttribute>() != null);

Loading…
Cancel
Save