The GroupBy and Select values are the same, and the parameter names are different. As a result, grouping fails

This commit is contained in:
sunkaixuan 2025-03-29 14:25:25 +08:00
parent ea38dd8413
commit b07668d308
3 changed files with 29 additions and 2 deletions

View File

@ -965,6 +965,11 @@ namespace SqlSugar
{ {
result = "*"; result = "*";
} }
if (result.StartsWith(UtilConstants.GroupReplaceKey))
{
this.GroupByIsReplace = true;
result = result.Replace(UtilConstants.GroupReplaceKey, string.Empty);
}
return result; return result;
} }

View File

@ -1,5 +1,6 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
using System.Text; using System.Text;
namespace SqlSugar namespace SqlSugar
@ -47,13 +48,33 @@ namespace SqlSugar
return this; return this;
} }
var orderObj = this.SqlBuilder.GroupByModelToSql(models); var orderObj = this.SqlBuilder.GroupByModelToSql(models);
this.GroupBy(orderObj.Key); if (orderObj.Value?.Length > 0 && this.Context.CurrentConnectionConfig?.DbType == DbType.SqlServer)
this.QueryBuilder.Parameters.AddRange(orderObj.Value); {
var groupBySql = UtilMethods.GetSqlString(DbType.SqlServer, orderObj.Key, orderObj.Value);
this.QueryBuilder.GroupBySql = groupBySql;
this.QueryBuilder.GroupBySqlOld = orderObj.Key;
this.QueryBuilder.GroupParameters = orderObj.Value.ToList();
this.GroupBy(orderObj.Key);
}
else
{
this.GroupBy(orderObj.Key);
this.QueryBuilder.Parameters.AddRange(orderObj.Value);
}
return this; return this;
} }
public ISugarQueryable<T> Select(List<SelectModel> models) public ISugarQueryable<T> Select(List<SelectModel> models)
{ {
var orderObj = this.SqlBuilder.SelectModelToSql(models); var orderObj = this.SqlBuilder.SelectModelToSql(models);
if (this.QueryBuilder.GroupParameters?.Any() == true && this.QueryBuilder.GroupBySql.HasValue())
{
var selectSql = UtilMethods.GetSqlString(DbType.SqlServer, orderObj.Key, UtilMethods.CopySugarParameters(orderObj.Value.ToList()).ToArray());
if (selectSql.Contains(this.QueryBuilder.GroupBySql))
{
this.Select(UtilConstants.GroupReplaceKey+selectSql);
return this;
}
}
this.Select(orderObj.Key); this.Select(orderObj.Key);
this.QueryBuilder.Parameters.AddRange(orderObj.Value); this.QueryBuilder.Parameters.AddRange(orderObj.Value);
return this; return this;

View File

@ -15,6 +15,7 @@ namespace SqlSugar
internal const string AssemblyName = "SqlSugar"; internal const string AssemblyName = "SqlSugar";
internal static string ReplaceKey = "{"+Guid.NewGuid()+"}"; internal static string ReplaceKey = "{"+Guid.NewGuid()+"}";
internal const string ReplaceCommaKey = "{112A689B-17A1-4A06-9D27-A39EAB8BC3D5}"; internal const string ReplaceCommaKey = "{112A689B-17A1-4A06-9D27-A39EAB8BC3D5}";
internal const string GroupReplaceKey = "{GroupReplaceKey_l33asdysaas1231s}";
internal static Type UShortType = typeof(ushort); internal static Type UShortType = typeof(ushort);
internal static Type ULongType = typeof(ulong); internal static Type ULongType = typeof(ulong);