From 2a7b832462c9ff2c4cab83eff98fd6fe0f9961cb Mon Sep 17 00:00:00 2001 From: sunkaixuan <610262374@qq.com> Date: Mon, 14 Aug 2017 00:01:01 +0800 Subject: [PATCH] - --- .../UnitTest/ExpressionTest/Method.cs | 21 +++++++-------- .../MethodCallExpressionResolve.cs | 26 ++++++++++++++----- 2 files changed, 29 insertions(+), 18 deletions(-) diff --git a/Src/Asp.Net/SqlServerTest/UnitTest/ExpressionTest/Method.cs b/Src/Asp.Net/SqlServerTest/UnitTest/ExpressionTest/Method.cs index c0a3c1ed3..eb772525e 100644 --- a/Src/Asp.Net/SqlServerTest/UnitTest/ExpressionTest/Method.cs +++ b/Src/Asp.Net/SqlServerTest/UnitTest/ExpressionTest/Method.cs @@ -725,18 +725,15 @@ namespace OrmTest.UnitTest private void IIF4() { - //Expression> exp = it => SqlFunc.IIF(true, it.Bool1, it.Bool2) == true; - //SqlServerExpressionContext expContext = new SqlServerExpressionContext(); - //expContext.Resolve(exp, ResolveExpressType.WhereSingle); - //var value = expContext.Result.GetString(); - //var pars = expContext.Parameters; - //base.Check(value, pars, "(( CASE WHEN ([Name] like '%'+@MethodConst0+'%') THEN @MethodConst1 ELSE @MethodConst2 END ) = @Const3 )", new List() - //{ - // new SugarParameter("@MethodConst0","a"), - // new SugarParameter("@MethodConst1",true), - // new SugarParameter("@MethodConst2",false), - // new SugarParameter("@Const3",true) - //}, "IIF4 error"); + Expression> exp = it => SqlFunc.IIF(true, it.Bool1, it.Bool2) == true; + SqlServerExpressionContext expContext = new SqlServerExpressionContext(); + expContext.Resolve(exp, ResolveExpressType.WhereSingle); + var value = expContext.Result.GetString(); + var pars = expContext.Parameters; + base.Check(value, pars, "(( CASE WHEN ( 1 = 1 ) THEN [Bool1] ELSE [Bool2] END ) = @Const0 )", new List() + { + new SugarParameter("@Const0",true) + }, "IIF4 error"); } } } diff --git a/Src/Asp.Net/SqlSugar/ExpressionsToSql/ResolveItems/MethodCallExpressionResolve.cs b/Src/Asp.Net/SqlSugar/ExpressionsToSql/ResolveItems/MethodCallExpressionResolve.cs index 6cb65eac0..68c24fc3a 100644 --- a/Src/Asp.Net/SqlSugar/ExpressionsToSql/ResolveItems/MethodCallExpressionResolve.cs +++ b/Src/Asp.Net/SqlSugar/ExpressionsToSql/ResolveItems/MethodCallExpressionResolve.cs @@ -146,19 +146,33 @@ namespace SqlSugar private void AppendItem(ExpressionParameter parameter, string name, IEnumerable args, MethodCallExpressionModel model, Expression item) { var isBinaryExpression = item is BinaryExpression || item is MethodCallExpression; - var isIFFBoolMember = name == "IIF" && (args.Last() is MemberExpression) && (args.Last() as MemberExpression).Type == PubConst.BoolType; - var isIFFUnary = name == "IIF" && (args.Last() is UnaryExpression) && (args.Last() as UnaryExpression).Operand.Type == PubConst.BoolType; - var isIFFBoolBinary = name == "IIF" && (args.Last() is BinaryExpression) && (args.Last() as BinaryExpression).Type == PubConst.BoolType; - if (isIFFUnary && item != args.First()) + var isConst = item is ConstantExpression; + var isIIF= name == "IIF"; + var isIFFBoolMember = isIIF && (item is MemberExpression) && (item as MemberExpression).Type == PubConst.BoolType; + var isIFFUnary = isIIF && (item is UnaryExpression) && (item as UnaryExpression).Operand.Type == PubConst.BoolType; + var isIFFBoolBinary = isIIF && (item is BinaryExpression) && (item as BinaryExpression).Type == PubConst.BoolType; + var isFirst = item == args.First(); + if (isFirst&& isIIF && isConst) + { + var value = (item as ConstantExpression).Value.ObjToBool() ? this.Context.DbMehtods.True() : this.Context.DbMehtods.False(); + var methodCallExpressionArgs = new MethodCallExpressionArgs() + { + IsMember =true, + MemberName = value, + MemberValue= value + }; + model.Args.Add(methodCallExpressionArgs); + } + else if (isIFFUnary && !isFirst) { AppendModelByIIFMember(parameter, model, (item as UnaryExpression).Operand); } - else if (isIFFBoolMember && item != args.First()) + else if (isIFFBoolMember && !isFirst) { AppendModelByIIFMember(parameter, model, item); } - else if (isIFFBoolBinary && item != args.First()) + else if (isIFFBoolBinary && !isFirst) { AppendModelByIIFBinary(parameter, model, item);