Skip to content

Commit

Permalink
- 修复 GroupBy 特殊情况下 AsProperty 无效的 bug;#1141
Browse files Browse the repository at this point in the history
  • Loading branch information
2881099 committed Jun 2, 2022
1 parent ee0f9f2 commit e2a4041
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
14 changes: 14 additions & 0 deletions FreeSql.Tests/FreeSql.Tests/Dameng/Curd/DamengSelectTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -868,6 +868,17 @@ public void GroupBy()
.OrderBy(a => a.Key)
.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
.GroupBy(a => a.Title)
.ToSql(b => new
Expand All @@ -876,6 +887,9 @@ public void GroupBy()
cou = b.Count(),
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
.GroupBy(a => a.Title)
.ToList(b => new
Expand Down
12 changes: 11 additions & 1 deletion FreeSql/Internal/CommonExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,17 @@ public bool ReadAnonymousField(List<SelectTableInfo> _tables, StringBuilder fiel
}
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;
diymemexp._map.CopyTo(parent);
parent.Property = parentProp; //若不加此行,会引用 GroupBy(..).ToList(a => new Dto { key = a.Key }) null 错误,CopyTo 之后 Property 变为 null
Expand Down

0 comments on commit e2a4041

Please sign in to comment.