From f6d3241d1d8d397643749dbd27ff712586a884b0 Mon Sep 17 00:00:00 2001 From: "610262374@qq.com" <610262374@qq.com> Date: Fri, 23 Nov 2018 14:08:54 +0800 Subject: [PATCH] SubQuery .Sum(it=>(int)it.XX) BUG --- Src/Asp.Net/SqlServerTest/Demos/1_Query.cs | 21 +++++++++++++++++++ .../ExpressionsToSql/Subquery/Items/SubMax.cs | 7 ++++++- .../ExpressionsToSql/Subquery/Items/SubMin.cs | 7 ++++++- .../ExpressionsToSql/Subquery/Items/SubSum.cs | 7 ++++++- 4 files changed, 39 insertions(+), 3 deletions(-) diff --git a/Src/Asp.Net/SqlServerTest/Demos/1_Query.cs b/Src/Asp.Net/SqlServerTest/Demos/1_Query.cs index 44f830d40..c0dba7555 100644 --- a/Src/Asp.Net/SqlServerTest/Demos/1_Query.cs +++ b/Src/Asp.Net/SqlServerTest/Demos/1_Query.cs @@ -94,6 +94,27 @@ namespace OrmTest.Demo Id = SqlFunc.Subqueryable().Where(s => s.Id == it.Id).Select(s => s.Id) }).ToList(); + var getAll6 = db.Queryable().Select(it => + new + { + name = it.Name, + id = SqlFunc.Subqueryable().Where(s => s.Id == it.Id).Sum(s => (int)s.SchoolId) + }).ToList(); + + var getAll66 = db.Queryable().Select(it => + new + { + name = it.Name, + id = SqlFunc.Subqueryable().Where(s => s.Id == it.Id).Sum(s =>s.SchoolId.Value) + }).ToList(); + + var getAll666 = db.Queryable().Select(it => + new + { + name = it.Name, + id = SqlFunc.Subqueryable().Where(s => s.Id == it.Id).Min(s => s.Id) + }).ToList(); + } private static void Async() diff --git a/Src/Asp.Net/SqlSugar/ExpressionsToSql/Subquery/Items/SubMax.cs b/Src/Asp.Net/SqlSugar/ExpressionsToSql/Subquery/Items/SubMax.cs index 137647595..cd8471be8 100644 --- a/Src/Asp.Net/SqlSugar/ExpressionsToSql/Subquery/Items/SubMax.cs +++ b/Src/Asp.Net/SqlSugar/ExpressionsToSql/Subquery/Items/SubMax.cs @@ -44,8 +44,13 @@ namespace SqlSugar { var exp = expression as MethodCallExpression; var argExp = exp.Arguments[0]; + var parametres = (argExp as LambdaExpression).Parameters; + if ((argExp as LambdaExpression).Body is UnaryExpression) + { + argExp = ((argExp as LambdaExpression).Body as UnaryExpression).Operand; + } var result = "MAX(" + SubTools.GetMethodValue(Context, argExp, ResolveExpressType.WhereMultiple) + ")"; - var selfParameterName = Context.GetTranslationColumnName((argExp as LambdaExpression).Parameters.First().Name) + UtilConstants.Dot; + var selfParameterName = Context.GetTranslationColumnName(parametres.First().Name) + UtilConstants.Dot; result = result.Replace(selfParameterName, string.Empty); return result; } diff --git a/Src/Asp.Net/SqlSugar/ExpressionsToSql/Subquery/Items/SubMin.cs b/Src/Asp.Net/SqlSugar/ExpressionsToSql/Subquery/Items/SubMin.cs index 5636b6a20..e76c9d77c 100644 --- a/Src/Asp.Net/SqlSugar/ExpressionsToSql/Subquery/Items/SubMin.cs +++ b/Src/Asp.Net/SqlSugar/ExpressionsToSql/Subquery/Items/SubMin.cs @@ -44,8 +44,13 @@ namespace SqlSugar { var exp = expression as MethodCallExpression; var argExp = exp.Arguments[0]; + var parametres = (argExp as LambdaExpression).Parameters; + if ((argExp as LambdaExpression).Body is UnaryExpression) + { + argExp = ((argExp as LambdaExpression).Body as UnaryExpression).Operand; + } var result = "MIN(" + SubTools.GetMethodValue(Context, argExp, ResolveExpressType.WhereMultiple) + ")"; - var selfParameterName = Context.GetTranslationColumnName((argExp as LambdaExpression).Parameters.First().Name) + UtilConstants.Dot; + var selfParameterName = Context.GetTranslationColumnName(parametres.First().Name) + UtilConstants.Dot; result = result.Replace(selfParameterName, string.Empty); return result; } diff --git a/Src/Asp.Net/SqlSugar/ExpressionsToSql/Subquery/Items/SubSum.cs b/Src/Asp.Net/SqlSugar/ExpressionsToSql/Subquery/Items/SubSum.cs index cd757de22..18003d965 100644 --- a/Src/Asp.Net/SqlSugar/ExpressionsToSql/Subquery/Items/SubSum.cs +++ b/Src/Asp.Net/SqlSugar/ExpressionsToSql/Subquery/Items/SubSum.cs @@ -44,8 +44,13 @@ namespace SqlSugar { var exp = expression as MethodCallExpression; var argExp = exp.Arguments[0]; + var parametres = (argExp as LambdaExpression).Parameters; + if ((argExp as LambdaExpression).Body is UnaryExpression) + { + argExp =((argExp as LambdaExpression).Body as UnaryExpression).Operand; + } var result = "SUM(" + SubTools.GetMethodValue(Context, argExp, ResolveExpressType.WhereMultiple)+")"; - var selfParameterName = Context.GetTranslationColumnName((argExp as LambdaExpression).Parameters.First().Name) + UtilConstants.Dot; + var selfParameterName = Context.GetTranslationColumnName(parametres.First().Name) + UtilConstants.Dot; result = result.Replace(selfParameterName, string.Empty); return result; }