From 36af40a868a6e1b15686bb8adebc4207215f918c Mon Sep 17 00:00:00 2001 From: 2881099 <2881099@qq.com> Date: Tue, 2 Jul 2024 23:41:03 +0800 Subject: [PATCH] =?UTF-8?q?-=20=E4=BC=98=E5=8C=96=20GroupBy=20=E8=81=9A?= =?UTF-8?q?=E5=90=88=E5=87=BD=E6=95=B0=20Count(bool)=20=E8=A7=A3=E6=9E=90?= =?UTF-8?q?=E6=88=90=20sum(case=20when)=EF=BC=9B#1841?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Examples/base_entity/Program.cs | 13 +++- FreeSql/FreeSql.xml | 109 --------------------------- FreeSql/Internal/CommonExpression.cs | 5 +- 3 files changed, 16 insertions(+), 111 deletions(-) diff --git a/Examples/base_entity/Program.cs b/Examples/base_entity/Program.cs index 0a3ad454e..37d9bc3ca 100644 --- a/Examples/base_entity/Program.cs +++ b/Examples/base_entity/Program.cs @@ -619,7 +619,18 @@ static void Main(string[] args) BaseEntity.Initialization(fsql, () => _asyncUow.Value); #endregion - + + var sqlc001 = fsql.Select() + .GroupBy(a => a.GroupId) + .ToSql(g => new + { + cou1 = g.Count(), + cou2 = g.Count(g.Value.Nickname), + cou3 = g.Count(g.Value.Nickname == "xx"), + cou4 = g.Count(g.Value.Sort > 50), + cou5 = g.Count(g.Value.Sort > 50 || g.Value.Username == "xx"), + }); + fsql.Select().As("aaa").Where(p => p.ID == 1).AsQueryable().Distinct().Select(p => new { p.Name, p.ID }).Count(); diff --git a/FreeSql/FreeSql.xml b/FreeSql/FreeSql.xml index a656490ce..a7a290881 100644 --- a/FreeSql/FreeSql.xml +++ b/FreeSql/FreeSql.xml @@ -1104,93 +1104,6 @@ - - - 动态创建实体类型 - - - - - 配置Class - - 类名 - 类标记的特性[Table(Name = "xxx")] [Index(xxxx)] - - - - - 获取类型构建器,可作为要构建的Type来引用 - - - - - 配置属性 - - 属性名称 - 属性类型 - 属性标记的特性-支持多个 - - - - - 配置属性 - - 属性名称 - 属性类型 - 该属性是否重写父类属性 - 属性标记的特性-支持多个 - - - - - 配置属性 - - 属性名称 - 属性类型 - 该属性是否重写父类属性 - 属性默认值 - 属性标记的特性-支持多个 - - - - - 配置父类 - - 父类类型 - - - - - Override属性 - - - - - - Emit动态创建出Class - Type - - - - - - Emit动态创建出Class - Type,不附带获取TableInfo - - - - - - 首字母小写 - - - - - - - 首字母大写 - - - - 获取实体的主键值,以 "*|_,[,_|*" 分割,当任意一个主键属性无值时,返回 "" @@ -5913,28 +5826,6 @@ 请使用 fsql.InsertDict(dict) 方法插入字典数据 - - - 动态构建Class Type - - - - - - 根据字典,创建 table 对应的实体对象 - - - - - - - - 根据实体对象,创建 table 对应的字典 - - - - - C#: that >= between && that <= and diff --git a/FreeSql/Internal/CommonExpression.cs b/FreeSql/Internal/CommonExpression.cs index e8cd97800..e2e979339 100644 --- a/FreeSql/Internal/CommonExpression.cs +++ b/FreeSql/Internal/CommonExpression.cs @@ -1194,7 +1194,10 @@ public string ExpressionLambdaToSql(Expression exp, ExpTSC tsc) { switch (exp3.Method.Name) { - case "Count": return exp3.Arguments.Count == 0 ? "count(1)" : $"count({ExpressionLambdaToSql(exp3.Arguments[0], tsc)})"; + case "Count": + if (exp3.Arguments.Count == 0) return "count(1)"; + if (exp3.Arguments[0].Type == typeof(bool)) return $"sum({_common.IIF(ExpressionLambdaToSql(exp3.Arguments[0], tsc), "1", "0")})"; + return $"count({ExpressionLambdaToSql(exp3.Arguments[0], tsc)})"; case "Sum": return $"sum({ExpressionLambdaToSql(exp3.Arguments[0], tsc)})"; case "Avg": return $"avg({ExpressionLambdaToSql(exp3.Arguments[0], tsc)})"; case "Max": return $"max({ExpressionLambdaToSql(exp3.Arguments[0], tsc)})";