mirror of
				https://github.com/nsnail/FreeSql.git
				synced 2025-11-04 17:20:49 +08:00 
			
		
		
		
	- 修复 GroupBy 特殊情况下 AsProperty 无效的 bug;#1141
This commit is contained in:
		@@ -868,6 +868,17 @@ WHERE (((a.""NAME"") in (SELECT s.""TITLE"" as1
 | 
			
		||||
                .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 @@ WHERE (((a.""NAME"") in (SELECT s.""TITLE"" as1
 | 
			
		||||
                    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
 | 
			
		||||
 
 | 
			
		||||
@@ -198,7 +198,17 @@ namespace FreeSql.Internal
 | 
			
		||||
                        }
 | 
			
		||||
                        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
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user