From ec83556743e6b224535d46a12a9d6e021d1c90e7 Mon Sep 17 00:00:00 2001 From: sunkaixuan <610262374@qq.com> Date: Mon, 8 Jan 2018 13:04:10 +0800 Subject: [PATCH] SqlFunc.IIF(Subquery) BUG --- .../UnitTest/Query/SelectQuery.cs | 12 ++++++++++ .../ResolveItems/BaseResolve.cs | 4 ++++ .../ExpressionsToSql/Subquery/SubResolve.cs | 23 ++++++++++++++++--- 3 files changed, 36 insertions(+), 3 deletions(-) diff --git a/Src/Asp.Net/SqlServerTest/UnitTest/Query/SelectQuery.cs b/Src/Asp.Net/SqlServerTest/UnitTest/Query/SelectQuery.cs index fc2769d33..736e35b72 100644 --- a/Src/Asp.Net/SqlServerTest/UnitTest/Query/SelectQuery.cs +++ b/Src/Asp.Net/SqlServerTest/UnitTest/Query/SelectQuery.cs @@ -106,6 +106,18 @@ namespace OrmTest.UnitTest new List() { new SugarParameter("@Const0",false) }, t6.Key, t6.Value, "select t6 Error"); + + + var t7 = db.Queryable().Select(it=>new DataTestInfo2() { + Bool1=SqlFunc.IIF(SqlFunc.Subqueryable().Where(x=>x.Id + ==it.Id).Any(),true,false) + }).ToSql(); + + base.Check("SELECT ( CASE WHEN (EXISTS ( SELECT * FROM [STudent] WHERE ( [ID] = [it].[ID] ) )) THEN @MethodConst0 ELSE @MethodConst1 END ) AS [Bool1] FROM [STudent] it ", + new List() { + new SugarParameter("@MethodConst0",true), + new SugarParameter("@MethodConst1",false) + }, t7.Key, t7.Value, "select t7 Error"); #endregion diff --git a/Src/Asp.Net/SqlSugar/ExpressionsToSql/ResolveItems/BaseResolve.cs b/Src/Asp.Net/SqlSugar/ExpressionsToSql/ResolveItems/BaseResolve.cs index d1685d092..c7f624b7e 100644 --- a/Src/Asp.Net/SqlSugar/ExpressionsToSql/ResolveItems/BaseResolve.cs +++ b/Src/Asp.Net/SqlSugar/ExpressionsToSql/ResolveItems/BaseResolve.cs @@ -264,6 +264,10 @@ namespace SqlSugar { this.Context.Parameters.AddRange(newContext.Parameters); } + if (newContext.SingleTableNameSubqueryShortName.HasValue()) + { + this.Context.SingleTableNameSubqueryShortName = newContext.SingleTableNameSubqueryShortName; + } var methodCallExpressionArgs = new MethodCallExpressionArgs() { IsMember = true, diff --git a/Src/Asp.Net/SqlSugar/ExpressionsToSql/Subquery/SubResolve.cs b/Src/Asp.Net/SqlSugar/ExpressionsToSql/Subquery/SubResolve.cs index acb5c29ac..4897cfd05 100644 --- a/Src/Asp.Net/SqlSugar/ExpressionsToSql/Subquery/SubResolve.cs +++ b/Src/Asp.Net/SqlSugar/ExpressionsToSql/Subquery/SubResolve.cs @@ -22,14 +22,31 @@ namespace SqlSugar this.context = context; var currentExpression = expression; allMethods.Add(currentExpression); - if (context.IsSingle && oppsiteExpression != null&& oppsiteExpression is MemberExpression) + if (context.IsSingle && oppsiteExpression != null && oppsiteExpression is MemberExpression) { var childExpression = (oppsiteExpression as MemberExpression).Expression; this.context.SingleTableNameSubqueryShortName = (childExpression as ParameterExpression).Name; } else if (context.IsSingle) { - this.context.SingleTableNameSubqueryShortName = (context.Expression as LambdaExpression).Parameters.First().Name; + if (context.Expression is LambdaExpression) + { + this.context.SingleTableNameSubqueryShortName = (context.Expression as LambdaExpression).Parameters.First().Name; + } + else if (context.Expression is MethodCallExpression) + { + var meExp = ((context.Expression as MethodCallExpression).Object as MethodCallExpression).Arguments[0] as LambdaExpression; + var selfParameterName = meExp.Parameters.First().Name; + context.SingleTableNameSubqueryShortName = (((meExp.Body as BinaryExpression).Left as MemberExpression).Expression as ParameterExpression).Name; + if (context.SingleTableNameSubqueryShortName == selfParameterName) + { + context.SingleTableNameSubqueryShortName = (((meExp.Body as BinaryExpression).Right as MemberExpression).Expression as ParameterExpression).Name; + } + } + else + { + Check.Exception(true, "I'm sorry I can't parse the current expression"); + } } while (currentExpression != null) { @@ -69,7 +86,7 @@ namespace SqlSugar isubList.Insert(0, new SubBegin()); if (isubList.Any(it => it is SubSelect)) { - isubList.Add(new SubTop() { Context=this.context }); + isubList.Add(new SubTop() { Context = this.context }); } if (isubList.Any(it => it is SubAny || it is SubNotAny)) {