private void MakeCallExpression(Expression expression) { if (expression.NodeType == ExpressionType.Call) { if (expressionPairs.Count > 0) { MethodCallExpression call = (MethodCallExpression)expression; var v1 = expressionPairs.Pop(); var v2 = expressionPairs.Pop(); var etype1 = v1.Value.GetType(); var etype2 = v2.Value.GetType(); if (call.Method.Name.Equals("Contains")) { if (v2.Type == ExpressionValueType.Field && etype1.IsGenericType && etype1.GetGenericTypeDefinition() == typeof(List <>)) { string fieldName = v2.Value.ToString(); object value = v1.Value; Condition c = new Condition(fieldName, value, parentXExpressionType.Count == 0 || parentXExpressionType.Peek() != ExpressionType.Not ? ConditionType.In : ConditionType.NotIn); if (condition == null) { condition = c; } if (currentNodes.Count > 0) { ((ConditionGroup)currentNodes.Peek()).Add(c); } return; } if (v1.Type == ExpressionValueType.Field && etype2.IsGenericType && etype2.GetGenericTypeDefinition() == typeof(List <>)) { string fieldName = v1.Value.ToString(); object value = v2.Value; Condition c = new Condition(fieldName, value, parentXExpressionType.Count == 0 || parentXExpressionType.Peek() != ExpressionType.Not ? ConditionType.In : ConditionType.NotIn); if (condition == null) { condition = c; } if (currentNodes.Count > 0) { ((ConditionGroup)currentNodes.Peek()).Add(c); } return; } } else if (call.Method.Name.Equals("Equals")) { if (v1.Type == ExpressionValueType.Field && v1.Type == ExpressionValueType.Field) { ExpressionCondition c = new ExpressionCondition( new BEExpression( new BEExpressionValue() { Value = v1.Value, Type = ExpressionValueType.Field }, new BEExpressionValue() { Value = v2.Value, Type = ExpressionValueType.Field }, Operator.Equals)); if (condition == null) { condition = c; } if (currentNodes.Count > 0) { ((ConditionGroup)currentNodes.Peek()).Add(c); } } else if (v1.Type == ExpressionValueType.Field) { Condition c = new Condition(v1.Value.ToString(), v2.Value, parentXExpressionType.Count == 0 || parentXExpressionType.Peek() != ExpressionType.Not ? ConditionType.Equals : ConditionType.NotEquals); if (condition == null) { condition = c; } if (currentNodes.Count > 0) { ((ConditionGroup)currentNodes.Peek()).Add(c); } } else if (v2.Type == ExpressionValueType.Field) { Condition c = new Condition(v2.Value.ToString(), v1.Value, parentXExpressionType.Count == 0 || parentXExpressionType.Peek() != ExpressionType.Not ? ConditionType.Equals : ConditionType.NotEquals); if (condition == null) { condition = c; } if (currentNodes.Count > 0) { ((ConditionGroup)currentNodes.Peek()).Add(c); } } } } } }
protected override Expression VisitBinary(BinaryExpression node) { parentXExpressionType.Push(node.NodeType); if (node.NodeType == ExpressionType.And || node.NodeType == ExpressionType.AndAlso) { AndCondition and = new AndCondition(); if (condition == null) { condition = and; } if (currentNodes.Count > 0) { ((ConditionGroup)currentNodes.Peek()).Add(and); } currentNodes.Push(and); Visit(node.Left); MakeCallExpression(node.Left); Visit(node.Right); MakeCallExpression(node.Right); currentNodes.Pop(); } else if (node.NodeType == ExpressionType.Or || node.NodeType == ExpressionType.OrElse) { OrCondition or = new OrCondition(); if (condition == null) { condition = or; } if (currentNodes.Count > 0) { ((ConditionGroup)currentNodes.Peek()).Add(or); } currentNodes.Push(or); Visit(node.Left); Visit(node.Right); currentNodes.Pop(); } else if (node.NodeType == ExpressionType.LessThan) { Visit(node.Left); Visit(node.Right); var c = CreateCondition(ConditionType.Lessthan); if (condition == null) { condition = c; } if (currentNodes.Count > 0) { ((ConditionGroup)currentNodes.Peek()).Add(c); } } else if (node.NodeType == ExpressionType.LessThanOrEqual) { Visit(node.Left); Visit(node.Right); var c = CreateCondition(ConditionType.LessthanEquals); if (condition == null) { condition = c; } if (currentNodes.Count > 0) { ((ConditionGroup)currentNodes.Peek()).Add(c); } } else if (node.NodeType == ExpressionType.GreaterThan) { Visit(node.Left); Visit(node.Right); var c = CreateCondition(ConditionType.Greater); if (condition == null) { condition = c; } if (currentNodes.Count > 0) { ((ConditionGroup)currentNodes.Peek()).Add(c); } } else if (node.NodeType == ExpressionType.GreaterThanOrEqual) { Visit(node.Left); Visit(node.Right); var c = CreateCondition(ConditionType.GreaterEquals); if (condition == null) { condition = c; } if (currentNodes.Count > 0) { ((ConditionGroup)currentNodes.Peek()).Add(c); } } else if (node.NodeType == ExpressionType.Equal) { Visit(node.Left); Visit(node.Right); var c = CreateCondition(parentXExpressionType.Count > 0 && parentXExpressionType.Peek() != ExpressionType.Not ? ConditionType.Equals : ConditionType.NotEquals); if (condition == null) { condition = c; } if (currentNodes.Count > 0) { ((ConditionGroup)currentNodes.Peek()).Add(c); } } else if (node.NodeType == ExpressionType.NotEqual) { Visit(node.Left); Visit(node.Right); var c = CreateCondition(parentXExpressionType.Count > 0 && parentXExpressionType.Peek() != ExpressionType.Not ? ConditionType.NotEquals : ConditionType.Equals); if (condition == null) { condition = c; } if (currentNodes.Count > 0) { ((ConditionGroup)currentNodes.Peek()).Add(c); } } else if (node.NodeType == ExpressionType.Add) { Visit(node.Left); Visit(node.Right); BEExpression expression = CreateExpression(Operator.Add); expressionPairs.Push(new BEExpressionValue() { Type = ExpressionValueType.Expression, Value = expression }); } else if (node.NodeType == ExpressionType.Subtract) { Visit(node.Left); Visit(node.Right); BEExpression expression = CreateExpression(Operator.Subtract); expressionPairs.Push(new BEExpressionValue() { Type = ExpressionValueType.Expression, Value = expression }); } else if (node.NodeType == ExpressionType.Multiply) { Visit(node.Left); Visit(node.Right); BEExpression expression = CreateExpression(Operator.Multiply); expressionPairs.Push(new BEExpressionValue() { Type = ExpressionValueType.Expression, Value = expression }); } else if (node.NodeType == ExpressionType.Divide) { Visit(node.Left); Visit(node.Right); BEExpression expression = CreateExpression(Operator.Divide); expressionPairs.Push(new BEExpressionValue() { Type = ExpressionValueType.Expression, Value = expression }); } else if (node.NodeType == ExpressionType.Modulo) { Visit(node.Left); Visit(node.Right); BEExpression expression = CreateExpression(Operator.Modulo); expressionPairs.Push(new BEExpressionValue() { Type = ExpressionValueType.Expression, Value = expression }); } else if (node.NodeType == ExpressionType.Power) { Visit(node.Left); Visit(node.Right); BEExpression expression = CreateExpression(Operator.Power); expressionPairs.Push(new BEExpressionValue() { Type = ExpressionValueType.Expression, Value = expression }); } else if (node.NodeType == ExpressionType.Call) { Visit(node.Left); Visit(node.Right); MakeCallExpression(node); } lastExpressionType = parentXExpressionType.Pop(); return(node); }