mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-12-05 03:17:41 +08:00
Optimize the group by
This commit is contained in:
@@ -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();
|
||||||
}
|
}
|
||||||
|
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);
|
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()
|
||||||
|
|||||||
@@ -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; }
|
||||||
@@ -919,6 +922,14 @@ namespace SqlSugar
|
|||||||
{
|
{
|
||||||
result = result + " AS columnName";
|
result = result + " AS columnName";
|
||||||
}
|
}
|
||||||
|
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;
|
this.SelectCacheKey = result;
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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);
|
||||||
|
|||||||
Reference in New Issue
Block a user