diff --git a/Src/Asp.Net/MySqlTest/Demos/1_Query.cs b/Src/Asp.Net/MySqlTest/Demos/1_Query.cs index df03c53af..31e9f127c 100644 --- a/Src/Asp.Net/MySqlTest/Demos/1_Query.cs +++ b/Src/Asp.Net/MySqlTest/Demos/1_Query.cs @@ -232,6 +232,12 @@ namespace OrmTest.Demo var getDay1List = db.Queryable().Where(it => it.CreateTime.Value.Hour == 1).ToList(); var getDateAdd = db.Queryable().Where(it => it.CreateTime.Value.AddDays(1) == DateTime.Now).ToList(); var getDateIsSame = db.Queryable().Where(it => SqlFunc.DateIsSame(DateTime.Now, DateTime.Now, DateType.Hour)).ToList(); + var test2 = db.Queryable((st, sc) => st.SchoolId == sc.Id) + .Where(st => + SqlFunc.IF(st.Id > 1) + .Return(st.Id) + .ElseIF(st.Id == 1) + .Return(st.SchoolId).End(st.Id) == 1).Select(st => st).ToList(); } public static void Page() { diff --git a/Src/Asp.Net/SqlServerTest/Demos/1_Query.cs b/Src/Asp.Net/SqlServerTest/Demos/1_Query.cs index 23f93a15c..34025c3c6 100644 --- a/Src/Asp.Net/SqlServerTest/Demos/1_Query.cs +++ b/Src/Asp.Net/SqlServerTest/Demos/1_Query.cs @@ -289,7 +289,7 @@ namespace OrmTest.Demo SqlFunc.IF(st.Id > 1) .Return(st.Id) .ElseIF(st.Id == 1) - .Return(st.SchoolId).End(st.Id) == 1).ToList(); + .Return(st.SchoolId).End(st.Id) == 1).Select(st=>st).ToList(); var test3 = db.Queryable().Select(it => it.Bool1).ToSql(); var test4 = db.Queryable().Select(it => new { b=it.Bool1 }).ToSql(); } diff --git a/Src/Asp.Net/SqlSugar/ExpressionsToSql/CaseWhen/CaseWhenResolve.cs b/Src/Asp.Net/SqlSugar/ExpressionsToSql/CaseWhen/CaseWhenResolve.cs index f557a6137..5afb90274 100644 --- a/Src/Asp.Net/SqlSugar/ExpressionsToSql/CaseWhen/CaseWhenResolve.cs +++ b/Src/Asp.Net/SqlSugar/ExpressionsToSql/CaseWhen/CaseWhenResolve.cs @@ -16,7 +16,6 @@ namespace SqlSugar { List allMethods = new List(); private ExpressionContext context = null; - private bool hasWhere; public CaseWhenResolve(MethodCallExpression expression, ExpressionContext context, Expression oppsiteExpression) { this.context = context; @@ -42,7 +41,17 @@ namespace SqlSugar public string GetSql() { - return ""; + allMethods.Reverse(); + List> sqls = new List>(); + foreach (var methodExp in allMethods) + { + var isFirst = allMethods.First() == methodExp; + var isLast= allMethods.Last() == methodExp; + var sql= SubTools.GetMethodValue(this.context, methodExp.Arguments[0],this.context.IsSingle?ResolveExpressType.WhereSingle:ResolveExpressType.WhereMultiple); + sqls.Add(new KeyValuePair(methodExp.Method.Name, sql)); + } + var result= this.context.DbMehtods.CaseWhen(sqls); + return result; } } } diff --git a/Src/Asp.Net/SqlSugar/ExpressionsToSql/DbMethods/DefaultDbMethod.cs b/Src/Asp.Net/SqlSugar/ExpressionsToSql/DbMethods/DefaultDbMethod.cs index 4320b9e52..6b0624d93 100644 --- a/Src/Asp.Net/SqlSugar/ExpressionsToSql/DbMethods/DefaultDbMethod.cs +++ b/Src/Asp.Net/SqlSugar/ExpressionsToSql/DbMethods/DefaultDbMethod.cs @@ -324,5 +324,29 @@ namespace SqlSugar { return "GETDATE()"; } + + public virtual string CaseWhen(List> sqls) + { + StringBuilder reslut = new StringBuilder(); + foreach (var item in sqls) + { + if (item.Key == "IF") + { + reslut.AppendFormat(" ( CASE WHEN {0} ", item.Value); + } + else if (item.Key == "End") + { + reslut.AppendFormat("ELSE {0} END )", item.Value); + } + else if (item.Key == "Return") + { + reslut.AppendFormat(" THEN {0} ", item.Value); + } + else { + reslut.AppendFormat(" WHEN {0} ", item.Value); + } + } + return reslut.ToString(); + } } } diff --git a/Src/Asp.Net/SqlSugar/ExpressionsToSql/DbMethods/IDbMethods.cs b/Src/Asp.Net/SqlSugar/ExpressionsToSql/DbMethods/IDbMethods.cs index 7452ec0a5..0e800a6ef 100644 --- a/Src/Asp.Net/SqlSugar/ExpressionsToSql/DbMethods/IDbMethods.cs +++ b/Src/Asp.Net/SqlSugar/ExpressionsToSql/DbMethods/IDbMethods.cs @@ -36,6 +36,7 @@ namespace SqlSugar string ToGuid(MethodCallExpressionModel model); string ToDouble(MethodCallExpressionModel model); string ToBool(MethodCallExpressionModel model); + string CaseWhen(List> sqls); string Substring(MethodCallExpressionModel model); string ToDate(MethodCallExpressionModel model); string ToTime(MethodCallExpressionModel model); diff --git a/Src/Asp.Net/SqlSugar/ExpressionsToSql/ResolveItems/MethodCallExpressionResolve.cs b/Src/Asp.Net/SqlSugar/ExpressionsToSql/ResolveItems/MethodCallExpressionResolve.cs index 4b4f3c45b..e40924746 100644 --- a/Src/Asp.Net/SqlSugar/ExpressionsToSql/ResolveItems/MethodCallExpressionResolve.cs +++ b/Src/Asp.Net/SqlSugar/ExpressionsToSql/ResolveItems/MethodCallExpressionResolve.cs @@ -56,7 +56,16 @@ namespace SqlSugar return; } else if (IsIfElse(express, methodName)) { - List expList = new List(); + CaseWhenResolve caseResole = new CaseWhenResolve(express, this.Context, parameter.OppsiteExpression); + var appendSql = caseResole.GetSql(); + if (this.Context.ResolveType.IsIn(ResolveExpressType.SelectMultiple, ResolveExpressType.SelectSingle)) + { + parameter.BaseParameter.CommonTempData = appendSql; + } + else + { + base.AppendValue(parameter, isLeft, appendSql); + } return; } if (IsContainsArray(express, methodName, isValidNativeMethod))