diff --git a/Src/Asp.NetCore2/SqlSugar/ExpressionsToSql/CaseWhen/CaseWhenResolve.cs b/Src/Asp.NetCore2/SqlSugar/ExpressionsToSql/CaseWhen/CaseWhenResolve.cs index f26107e67..a4ce59493 100644 --- a/Src/Asp.NetCore2/SqlSugar/ExpressionsToSql/CaseWhen/CaseWhenResolve.cs +++ b/Src/Asp.NetCore2/SqlSugar/ExpressionsToSql/CaseWhen/CaseWhenResolve.cs @@ -48,22 +48,33 @@ namespace SqlSugar { var isFirst = allMethods.First() == methodExp; var isLast = allMethods.Last() == methodExp; + var isIsNegate = false; if (methodExp.Arguments.Count == 0) { sqls.Add(new KeyValuePair(methodExp.Method.Name, "null")); } else { - var sql = SubTools.GetMethodValue(this.context, methodExp.Arguments[0], this.context.IsSingle ? ResolveExpressType.WhereSingle : ResolveExpressType.WhereMultiple); - if (methodExp.Method.Name == "IF") + var exp = methodExp.Arguments[0]; + if (ExpressionTool.IsNegate(exp)) + { + isIsNegate = true; + exp = (exp as UnaryExpression).Operand; + } + var sql = SubTools.GetMethodValue(this.context, exp, this.context.IsSingle ? ResolveExpressType.WhereSingle : ResolveExpressType.WhereMultiple); + if (methodExp.Method.Name == "IF") { var parameter = this.context.Parameters.FirstOrDefault(it => it.ParameterName == sql.Trim()); - if (parameter!=null&¶meter.Value is bool) + if (parameter!=null&¶meter.Value is bool) { sql = Convert.ToBoolean(parameter.Value) ? " 1=1 " : " 1=2 "; this.context.Parameters.Remove(parameter); } } + if (isIsNegate) + { + sql = " ("+sql + "*-1) "; + } sqls.Add(new KeyValuePair(methodExp.Method.Name, sql)); } } diff --git a/Src/Asp.NetCore2/SqlSugar/ExpressionsToSql/Common/ExpressionTool.cs b/Src/Asp.NetCore2/SqlSugar/ExpressionsToSql/Common/ExpressionTool.cs index d6b8eec2c..63fa11ef8 100644 --- a/Src/Asp.NetCore2/SqlSugar/ExpressionsToSql/Common/ExpressionTool.cs +++ b/Src/Asp.NetCore2/SqlSugar/ExpressionsToSql/Common/ExpressionTool.cs @@ -617,5 +617,11 @@ namespace SqlSugar } return result; } + + + public static bool IsNegate(Expression exp) + { + return exp is UnaryExpression && exp.NodeType == ExpressionType.Negate; + } } }