CRLExpression BinaryExpressionHandler(Expression left, Expression right, ExpressionType type, int level = 1) { var types = new ExpressionType[] { ExpressionType.Equal, ExpressionType.GreaterThan, ExpressionType.GreaterThanOrEqual, ExpressionType.LessThan, ExpressionType.LessThanOrEqual, ExpressionType.NotEqual }; var leftPar = RouteExpressionHandler(left); var isRight = types.Contains(type); var rightPar = RouteExpressionHandler(right, isRight, level); var e = new CRLExpression() { ExpressionType = type.ToString(), Left = leftPar, Right = rightPar, Type = isRight ? CRLExpressionType.Binary : CRLExpressionType.Tree }; return(e); }
Expression <Func <T, bool> > CreateLambdaTree(CRLExpression expression) { var expression1 = CreateLambda(expression.Left); var expression2 = CreateLambda(expression.Right); if (expression.ExpressionType == "AndAlso") { return(expression1.Compose(expression2, Expression.AndAlso)); } else { return(expression1.Compose(expression2, Expression.OrElse)); } return(null); }
/// <summary> /// 转换为Lambda表达式 /// </summary> /// <param name="expression"></param> /// <returns></returns> public Expression <Func <T, bool> > CreateLambda(CRLExpression expression) { if (expression.Type == CRLExpressionType.Tree) { return(CreateLambdaTree(expression)); } else if (expression.Type == CRLExpressionType.MethodCall) { return(CreateLambdaMethodCall(expression)); } else { return(CreateLambdaBinary(expression)); } }
internal MethodCallExpression CreateLambdaMethodCall2(CRLExpression expression) { var creater = new LambdaCreater <T>(); var arry = expression.Data.ToString().Split('|'); var propertyName = arry[0]; var methodName = arry[1]; var args = expression.Data.ToString().Substring(propertyName.Length + methodName.Length + 2); var dic = new Dictionary <string, MethodResultHandler>(); dic.Add("Substring", creater.Substring); dic.Add("IndexOf", creater.IndexOf); if (!dic.ContainsKey(methodName)) { throw new Exception("没有对应的方法 " + methodName); } return(dic[methodName](propertyName, args.Split(','))); }
Expression <Func <T, bool> > CreateLambdaMethodCall(CRLExpression expression) { var creater = new LambdaCreater <T>(); var arry = expression.Data.ToString().Split('|'); var propertyName = arry[0]; var methodName = arry[1]; var args = expression.Data.ToString().Substring(propertyName.Length + methodName.Length + 2); var dic = new Dictionary <string, MethodHandler>(); dic.Add("Like", creater.Contains); dic.Add("Contains", creater.Contains); dic.Add("StartsWith", creater.StartsWith); dic.Add("EndsWith", creater.EndsWith); if (!dic.ContainsKey(methodName)) { throw new Exception("没有对应的方法 " + methodName); } return(dic[methodName](propertyName, args.Split(','))); }
Expression <Func <T, bool> > CreateLambdaBinary(CRLExpression expression) { var left = expression.Left; var right = expression.Right; var creater = new LambdaCreater <T>(); var type = expression.ExpressionType; if (left.Type == CRLExpressionType.MethodCall)//按方法 如 Substring,Indexof { var dic = new Dictionary <string, BinaryMethodHandler>(); dic.Add("Equal", creater.Equal); dic.Add("NotEqual", creater.NotEqual); dic.Add("Greater", creater.Greater); dic.Add("Less", creater.Less); dic.Add("GreaterThan", creater.GreaterThan); dic.Add("LessThan", creater.LessThan); if (!dic.ContainsKey(type)) { throw new Exception("没有对应的运算方法 " + type); } var left2 = CreateLambdaMethodCall2(left); var value = ObjectConvert.ConvertObject(left2.Type, right.Data); return(dic[type](left2, value)); } else { var dic = new Dictionary <string, BinaryHandler>(); dic.Add("Equal", creater.Equal); dic.Add("NotEqual", creater.NotEqual); dic.Add("Greater", creater.Greater); dic.Add("Less", creater.Less); dic.Add("GreaterThan", creater.GreaterThan); dic.Add("LessThan", creater.LessThan); if (!dic.ContainsKey(type)) { throw new Exception("没有对应的运算方法 " + type); } return(dic[type](left.Data.ToString(), right.Data)); } }