Exemplo n.º 1
0
 public string ParseLambdaExpression <TEntity>(Expression <Func <TEntity, bool> > expr)
 {
     lock (_locker)
     {
         _variables = null;
         return(ParseExpressionTree(string.Empty, ExpressionTreeHelpers.ToBinary(expr.Body)));
     }
 }
Exemplo n.º 2
0
        private string ParseExpressionTree(string result, BinaryExpression bin)
        {
            if (bin.NodeType == ExpressionType.OrElse || bin.NodeType == ExpressionType.Or)
            {
                return($"{result}({ParseExpressionTree(result, ExpressionTreeHelpers.ToBinary(bin.Left))}) or ({ParseExpressionTree(result, ExpressionTreeHelpers.ToBinary(bin.Right))})");
            }

            if (bin.NodeType == ExpressionType.AndAlso || bin.NodeType == ExpressionType.And)
            {
                return($"{result}({ParseExpressionTree(result, ExpressionTreeHelpers.ToBinary(bin.Left))}) and ({ParseExpressionTree(result, ExpressionTreeHelpers.ToBinary(bin.Right))})");
            }

            if (ExpressionTreeHelpers.Operators.Contains(bin.NodeType))
            {
                string lhs = bin.Left.ToString(), rhs = bin.Right.ToString();

                if (bin.Right is MemberExpression member)
                {
                    EvaluateMember(member, bin, out rhs);
                }

                if (bin.Left.NodeType == ExpressionType.Convert || bin.Right.NodeType == ExpressionType.Convert)
                {
                    ConvertNode(bin, out lhs, out rhs);
                }

                if (bin.Right.NodeType == ExpressionType.Not)
                {
                    rhs = HandleNotExpression(bin);
                }

                return($"{result}{lhs} {ExpressionTreeHelpers.GetOperator(bin.NodeType)} {rhs?.Replace(@"""", "'")}");
            }

            return(string.Empty);
        }