- 修复 GroupBy 特殊情况下 AsProperty 无效的 bug;#1141

This commit is contained in:
2881099 2022-06-02 19:20:37 +08:00
parent ee0f9f2c11
commit e2a4041590
2 changed files with 25 additions and 1 deletions

View File

@ -868,6 +868,17 @@ WHERE (((a.""NAME"") in (SELECT s.""TITLE"" as1
.OrderBy(a => a.Key) .OrderBy(a => a.Key)
.ToList(a => a.Sum(a.Value.TypeGuid)); .ToList(a => a.Sum(a.Value.TypeGuid));
var aggsql110 = select
.GroupBy(a => a.Title)
.ToSql(b => new
{
tit = b.Key,
cou = b.Count(),
sum2 = b.Sum(b.Value.TypeGuid)
}, FieldAliasOptions.AsProperty);
Assert.Equal(@"SELECT a.""TITLE"" ""TIT"", count(1) ""COU"", sum(a.""TYPEGUID"") ""SUM2""
FROM ""TB_TOPIC22"" a
GROUP BY a.""TITLE""", aggsql110);
var aggsql1 = select var aggsql1 = select
.GroupBy(a => a.Title) .GroupBy(a => a.Title)
.ToSql(b => new .ToSql(b => new
@ -876,6 +887,9 @@ WHERE (((a.""NAME"") in (SELECT s.""TITLE"" as1
cou = b.Count(), cou = b.Count(),
sum2 = b.Sum(b.Value.TypeGuid) sum2 = b.Sum(b.Value.TypeGuid)
}); });
Assert.Equal(@"SELECT a.""TITLE"" as1, count(1) as2, sum(a.""TYPEGUID"") as3
FROM ""TB_TOPIC22"" a
GROUP BY a.""TITLE""", aggsql1);
var aggtolist1 = select var aggtolist1 = select
.GroupBy(a => a.Title) .GroupBy(a => a.Title)
.ToList(b => new .ToList(b => new

View File

@ -199,6 +199,16 @@ namespace FreeSql.Internal
if (diymemexp != null && exp is MemberExpression expMem2 && expMem2.Member.Name == "Key" && expMem2.Expression.Type.FullName.StartsWith("FreeSql.ISelectGroupingAggregate`")) if (diymemexp != null && exp is MemberExpression expMem2 && expMem2.Member.Name == "Key" && expMem2.Expression.Type.FullName.StartsWith("FreeSql.ISelectGroupingAggregate`"))
{ {
field.Append(diymemexp._field); field.Append(diymemexp._field);
if (diymemexp._map.Childs.Any() == false) //处理 GroupBy(a => a.Title) ToSql(g => new { tit = a.Key }, FieldAliasOptions.AsProperty) 问题
{
if (index >= 0) field.Append(_common.FieldAsAlias($"as{++index}"));
else if (index == ReadAnonymousFieldAsCsName)
{
var csname = GetFieldAsCsName(parent.CsName);
if (diymemexp._field.EndsWith(csname, StringComparison.CurrentCultureIgnoreCase) == false) //DbField 和 CsName 相同的时候,不处理
field.Append(_common.FieldAsAlias(csname));
}
}
var parentProp = parent.Property; var parentProp = parent.Property;
diymemexp._map.CopyTo(parent); diymemexp._map.CopyTo(parent);
parent.Property = parentProp; //若不加此行,会引用 GroupBy(..).ToList(a => new Dto { key = a.Key }) null 错误CopyTo 之后 Property 变为 null parent.Property = parentProp; //若不加此行,会引用 GroupBy(..).ToList(a => new Dto { key = a.Key }) null 错误CopyTo 之后 Property 变为 null