using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; namespace SqlSugar { public class CaseWhenResolve { List allMethods = new List(); private ExpressionContext context = null; public CaseWhenResolve(MethodCallExpression expression, ExpressionContext context, Expression oppsiteExpression) { this.context = context; var currentExpression = expression; allMethods.Add(currentExpression); 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; } while (currentExpression != null) { var addItem = currentExpression.Object as MethodCallExpression; if (addItem != null) allMethods.Add(addItem); currentExpression = addItem; } } public string GetSql() { allMethods.Reverse(); var 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; } } }