From e2a4041590d99ab26823a63f632dc14590780fca Mon Sep 17 00:00:00 2001 From: 2881099 <2881099@qq.com> Date: Thu, 2 Jun 2022 19:20:37 +0800 Subject: [PATCH] =?UTF-8?q?-=20=E4=BF=AE=E5=A4=8D=20GroupBy=20=E7=89=B9?= =?UTF-8?q?=E6=AE=8A=E6=83=85=E5=86=B5=E4=B8=8B=20AsProperty=20=E6=97=A0?= =?UTF-8?q?=E6=95=88=E7=9A=84=20bug=EF=BC=9B#1141?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../FreeSql.Tests/Dameng/Curd/DamengSelectTest.cs | 14 ++++++++++++++ FreeSql/Internal/CommonExpression.cs | 12 +++++++++++- 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/FreeSql.Tests/FreeSql.Tests/Dameng/Curd/DamengSelectTest.cs b/FreeSql.Tests/FreeSql.Tests/Dameng/Curd/DamengSelectTest.cs index 11f712fbf..0625c6708 100644 --- a/FreeSql.Tests/FreeSql.Tests/Dameng/Curd/DamengSelectTest.cs +++ b/FreeSql.Tests/FreeSql.Tests/Dameng/Curd/DamengSelectTest.cs @@ -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 @@ -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 diff --git a/FreeSql/Internal/CommonExpression.cs b/FreeSql/Internal/CommonExpression.cs index f12320984..c806edd36 100644 --- a/FreeSql/Internal/CommonExpression.cs +++ b/FreeSql/Internal/CommonExpression.cs @@ -198,7 +198,17 @@ public bool ReadAnonymousField(List _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