From 58bb0667c3553b15463e7464e089953e3a4ba752 Mon Sep 17 00:00:00 2001 From: 2881099 <2881099@qq.com> Date: Tue, 25 Feb 2025 21:20:07 +0800 Subject: [PATCH] =?UTF-8?q?-=20=E4=BF=AE=E5=A4=8D=20ISelectGroupingAggrega?= =?UTF-8?q?te=20=E8=87=AA=E5=AE=9A=E4=B9=89=E8=A7=A3=E6=9E=90=E6=96=B9?= =?UTF-8?q?=E6=B3=95=E6=8A=A5=E9=94=99=E7=9A=84=E9=97=AE=E9=A2=98=EF=BC=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Examples/base_entity/Program.cs | 23 +++++++++++++++++++++++ FreeSql/Internal/CommonExpression.cs | 1 + 2 files changed, 24 insertions(+) diff --git a/Examples/base_entity/Program.cs b/Examples/base_entity/Program.cs index ab4f15a69..6e06396d5 100644 --- a/Examples/base_entity/Program.cs +++ b/Examples/base_entity/Program.cs @@ -13,6 +13,7 @@ using NetTopologySuite.Geometries; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using Npgsql; +using Org.BouncyCastle.Tls; using System; using System.Collections; using System.Collections.Concurrent; @@ -620,6 +621,18 @@ namespace base_entity BaseEntity.Initialization(fsql, () => _asyncUow.Value); #endregion + var sql20250205 = fsql.Select() + .InnerJoin((l, p) => l.ProductId == p.ID) + .GroupBy((l, p) => new { p.ID, ShopType = l.ShopType ?? 0 }) + .ToSql(x => new + { + TradeId = x.Key.ID, + ShopType = x.Key.ShopType, + FieldCount = x.CountDistinct(x.Value.Item2.ID), + Count = x.Count(x.Value.Item1.Id), + Kb = (long)x.Sum(x.Value.Item1.Amount) + }); + var res = fsql.Select() .Where(x => x.Date >= 20230101 && x.Date < 20240101 && x.ScanCode > 0) .Where(x => @@ -3510,4 +3523,14 @@ public sealed class MemberActionDayCountModel /// public decimal ScanCodeAmount { get; set; } #endregion +} +[ExpressionCall] +public static class ExpressionCallExtesions +{ + static ThreadLocal context = new ThreadLocal(); + public static int CountDistinct(this ISelectGroupingAggregate that, object column) + { + context.Value.Result = $"count(distinct {context.Value.ParsedContent["column"]})"; + return 0; + } } \ No newline at end of file diff --git a/FreeSql/Internal/CommonExpression.cs b/FreeSql/Internal/CommonExpression.cs index 3f2e98b40..d193663b2 100644 --- a/FreeSql/Internal/CommonExpression.cs +++ b/FreeSql/Internal/CommonExpression.cs @@ -2055,6 +2055,7 @@ namespace FreeSql.Internal { var expStackFirst = expStack.First() as ParameterExpression; var bidx = expStackFirst.Type.FullName.StartsWith("FreeSql.ISelectGroupingAggregate`") ? 2 : 1; //.Key .Value + if (bidx == 2 && expStack.Count == 1) return ""; //不解析 var diyexpMembers = expStack.Where((a, b) => b >= bidx).ToArray(); if (diyexpMembers.Any() == false && diymemexp != null && diymemexp is Select0Provider.WithTempQueryParser tempQueryParser && tempQueryParser.GetOutsideSelectTable(expStackFirst) != null) diyexpMembers = expStack.ToArray();