mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-04-05 17:37:58 +08:00
Optimize the group by
This commit is contained in:
parent
44738faa32
commit
b0d995a6b3
@ -1731,6 +1731,8 @@ namespace SqlSugar
|
|||||||
}
|
}
|
||||||
protected ISugarQueryable<T> _GroupBy(Expression expression)
|
protected ISugarQueryable<T> _GroupBy(Expression expression)
|
||||||
{
|
{
|
||||||
|
var oldParameterNames = this.QueryBuilder.Parameters?.Select(it => it.ParameterName)
|
||||||
|
?.ToList();
|
||||||
QueryBuilder.CheckExpression(expression, "GroupBy");
|
QueryBuilder.CheckExpression(expression, "GroupBy");
|
||||||
LambdaExpression lambda = expression as LambdaExpression;
|
LambdaExpression lambda = expression as LambdaExpression;
|
||||||
expression = lambda.Body;
|
expression = lambda.Body;
|
||||||
@ -1761,7 +1763,19 @@ namespace SqlSugar
|
|||||||
lamResult = QueryBuilder.GetExpressionValue(expression, isSingle ? ResolveExpressType.FieldSingle : ResolveExpressType.FieldMultiple);
|
lamResult = QueryBuilder.GetExpressionValue(expression, isSingle ? ResolveExpressType.FieldSingle : ResolveExpressType.FieldMultiple);
|
||||||
result = lamResult.GetResultString();
|
result = lamResult.GetResultString();
|
||||||
}
|
}
|
||||||
GroupBy(result);
|
if (oldParameterNames != null && this.Context.CurrentConnectionConfig.DbType == DbType.SqlServer)
|
||||||
|
{
|
||||||
|
var newParas = this.QueryBuilder.Parameters.Where(it => !oldParameterNames.Contains(it.ParameterName)).ToList();
|
||||||
|
this.QueryBuilder.GroupParameters = newParas;
|
||||||
|
var groupBySql = UtilMethods.GetSqlString(DbType.SqlServer, result, newParas.ToArray());
|
||||||
|
this.QueryBuilder.GroupBySql = groupBySql;
|
||||||
|
this.QueryBuilder.GroupBySqlOld = result;
|
||||||
|
GroupBy(result);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
GroupBy(result);
|
||||||
|
}
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
protected ISugarQueryable<T> _As(string tableName, string entityName)
|
protected ISugarQueryable<T> _As(string tableName, string entityName)
|
||||||
@ -2142,6 +2156,9 @@ namespace SqlSugar
|
|||||||
asyncQueryableBuilder.Hints = this.QueryBuilder.Hints;
|
asyncQueryableBuilder.Hints = this.QueryBuilder.Hints;
|
||||||
asyncQueryableBuilder.MasterDbTableName = this.QueryBuilder.MasterDbTableName;
|
asyncQueryableBuilder.MasterDbTableName = this.QueryBuilder.MasterDbTableName;
|
||||||
asyncQueryableBuilder.IsParameterizedConstructor = this.QueryBuilder.IsParameterizedConstructor;
|
asyncQueryableBuilder.IsParameterizedConstructor = this.QueryBuilder.IsParameterizedConstructor;
|
||||||
|
asyncQueryableBuilder.GroupParameters = this.QueryBuilder.GroupParameters;
|
||||||
|
asyncQueryableBuilder.GroupBySql = this.QueryBuilder.GroupBySql;
|
||||||
|
asyncQueryableBuilder.GroupBySqlOld = this.QueryBuilder.GroupBySqlOld;
|
||||||
if (this.QueryBuilder.AppendNavInfo != null)
|
if (this.QueryBuilder.AppendNavInfo != null)
|
||||||
{
|
{
|
||||||
asyncQueryableBuilder.AppendNavInfo = new AppendNavInfo()
|
asyncQueryableBuilder.AppendNavInfo = new AppendNavInfo()
|
||||||
|
@ -1341,7 +1341,7 @@ namespace SqlSugar
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
public virtual ISugarQueryable<T> GroupBy(Expression<Func<T, object>> expression)
|
public virtual ISugarQueryable<T> GroupBy(Expression<Func<T, object>> expression)
|
||||||
{
|
{
|
||||||
_GroupBy(expression);
|
_GroupBy(expression);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
@ -34,6 +34,9 @@ namespace SqlSugar
|
|||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region Splicing basic
|
#region Splicing basic
|
||||||
|
public List<SugarParameter> GroupParameters { get; set; }
|
||||||
|
public string GroupBySql { get; set; }
|
||||||
|
public string GroupBySqlOld { get; set; }
|
||||||
public Type AsType { get; set; }
|
public Type AsType { get; set; }
|
||||||
public bool IsParameterizedConstructor { get; set; }
|
public bool IsParameterizedConstructor { get; set; }
|
||||||
public string Hints { get; set; }
|
public string Hints { get; set; }
|
||||||
@ -283,7 +286,7 @@ namespace SqlSugar
|
|||||||
};
|
};
|
||||||
resolveExpress.Resolve(expression, resolveType);
|
resolveExpress.Resolve(expression, resolveType);
|
||||||
this.Parameters.AddRange(resolveExpress.Parameters.Select(it => new SugarParameter(it.ParameterName, it.Value, it.DbType) { Size=it.Size,TypeName=it.TypeName, IsNvarchar2=it.IsNvarchar2}));
|
this.Parameters.AddRange(resolveExpress.Parameters.Select(it => new SugarParameter(it.ParameterName, it.Value, it.DbType) { Size=it.Size,TypeName=it.TypeName, IsNvarchar2=it.IsNvarchar2}));
|
||||||
var result = resolveExpress.Result;
|
var result = resolveExpress.Result;
|
||||||
var isSingleTableHasSubquery = IsSingle() && resolveExpress.SingleTableNameSubqueryShortName.HasValue();
|
var isSingleTableHasSubquery = IsSingle() && resolveExpress.SingleTableNameSubqueryShortName.HasValue();
|
||||||
if (isSingleTableHasSubquery)
|
if (isSingleTableHasSubquery)
|
||||||
{
|
{
|
||||||
@ -919,7 +922,15 @@ namespace SqlSugar
|
|||||||
{
|
{
|
||||||
result = result + " AS columnName";
|
result = result + " AS columnName";
|
||||||
}
|
}
|
||||||
this.SelectCacheKey = result;
|
if (this.GroupParameters?.Any()==true && this.GroupBySql.HasValue())
|
||||||
|
{
|
||||||
|
var selectSql = UtilMethods.GetSqlString(DbType.SqlServer, result, UtilMethods.CopySugarParameters(this.Parameters).ToArray());
|
||||||
|
if (selectSql.Contains(this.GroupBySql))
|
||||||
|
{
|
||||||
|
result = selectSql;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
this.SelectCacheKey = result;
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -1090,7 +1101,7 @@ namespace SqlSugar
|
|||||||
public bool NoCheckInclude { get; set; }
|
public bool NoCheckInclude { get; set; }
|
||||||
public virtual bool IsSelectNoAll { get; set; } = false;
|
public virtual bool IsSelectNoAll { get; set; } = false;
|
||||||
public List<string> AutoAppendedColumns { get; set; }
|
public List<string> AutoAppendedColumns { get; set; }
|
||||||
public Dictionary<string, string> MappingKeys { get; set; }
|
public Dictionary<string, string> MappingKeys { get; set; }
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
private string GetTableName(string entityName)
|
private string GetTableName(string entityName)
|
||||||
|
@ -69,6 +69,10 @@ namespace SqlSugar
|
|||||||
if (isFirst && oldOrderByValue == "ORDER BY GETDATE() ") { this.OrderByValue = null; }
|
if (isFirst && oldOrderByValue == "ORDER BY GETDATE() ") { this.OrderByValue = null; }
|
||||||
var rowNumberString = string.Format(",ROW_NUMBER() OVER({0}) AS RowIndex ", GetOrderByString);
|
var rowNumberString = string.Format(",ROW_NUMBER() OVER({0}) AS RowIndex ", GetOrderByString);
|
||||||
string groupByValue = GetGroupByString + HavingInfos;
|
string groupByValue = GetGroupByString + HavingInfos;
|
||||||
|
if (this.GroupParameters?.Any()==true)
|
||||||
|
{
|
||||||
|
groupByValue = groupByValue.Replace(this.GroupBySqlOld,this.GroupBySql);
|
||||||
|
}
|
||||||
string orderByValue = (!isRowNumber && this.OrderByValue.HasValue()) ? GetOrderByString : null;
|
string orderByValue = (!isRowNumber && this.OrderByValue.HasValue()) ? GetOrderByString : null;
|
||||||
if (isIgnoreOrderBy) { orderByValue = null; }
|
if (isIgnoreOrderBy) { orderByValue = null; }
|
||||||
sql.AppendFormat(SqlTemplate, GetSelect(isFirst,isTop), base.GetTableNameString, base.GetWhereValueString, groupByValue, orderByValue);
|
sql.AppendFormat(SqlTemplate, GetSelect(isFirst,isTop), base.GetTableNameString, base.GetWhereValueString, groupByValue, orderByValue);
|
||||||
|
Loading…
Reference in New Issue
Block a user