How do I extract the values of an Expression?

8

Hello. Is it possible to extract from an Expression what filters, computers, etc. were used in a query?

Example:

public class Program
{
    class Produto
    {
        public int Id;
        public string Nome;

        public Produto(int id, string nome)
        {
            this.Id = id;
            this.Nome = nome;
        }
    }

    public static void Main()
    {
        List<Produto> produtos = new List<Produto>();
        produtos.Add(new Produto(1, "Arroz"));
        produtos.Add(new Produto(2, "Feijão"));
        produtos.Add(new Produto(3, "Trigo"));
        produtos.Add(new Produto(4, "Batata"));

        var expression = (from produto in produtos
                          where produto.Id < 3
                          select produto).AsQueryable().Expression;
    }
}

In the case, what I need is to extract from expression that the filter used was Id < 3

    
asked by anonymous 06.10.2015 / 22:15

1 answer

3

How I told you the question previous , There are several types of Expressions , which requires you to place additional logic to retrieve the values from your filter.

Typically, this parameter Expression is a BinaryExpression . Its components can be obtained as follows:

var expression = meuQueryable.Expression as BinaryExpression;

BinaryExpressions usually have Left and Right which, in turn, are also Expressions . A code that I use to test what type of expression is Left and Right (not very good, I'm still perfecting it) is as follows:

            switch (expression.Left.GetType().ToString())
            {
                case "System.Linq.Expressions.LogicalBinaryExpression":
                case "System.Linq.Expressions.MethodBinaryExpression":
                    valorFinalEsquerdo = Condicao(expression.Left);
                    break;
                case "System.Linq.Expressions.UnaryExpression":
                    var body2 = expression.Left as UnaryExpression;

                    if (body2 != null)
                    {
                        var left2 = body2.Operand as MemberExpression;
                        if (left2 != null)
                        {
                            var teste = ReflectionUtils.ExtrairAtributoColumnDeMember(left2.Member);
                            valorFinalEsquerdo = teste.Name;
                        }
                    }
                    break;
                default:
                    var left = expression.Left as MemberExpression;
                    if (left != null)
                    {
                        var teste = ReflectionUtils.ExtrairAtributoColumnDeMember(left.Member);
                        valorFinalEsquerdo = teste.Name;
                    }
                    break;
            }

            switch (expression.Right.GetType().ToString())
            {
                case "System.Linq.Expressions.MethodBinaryExpression":
                    valorFinalDireito = Condicao(expression.Right);
                    break;
                case "System.Linq.Expressions.PropertyExpression":
                        var right2 = expression.Right as MemberExpression;
                        if (right2 != null)
                        {
                            valorFinalDireito = AvaliarExpressao(right2);
                        }

                    break;
                default:
                    var right = expression.Right as ConstantExpression;
                    if (right != null)
                    {
                        valorFinalDireito = right.Value;
                    }
                    break;
            }

The code is part of a function called Condicao , which I call recursively. The values are obtained within valorFinalDireito and valorFinalEsquerdo .

    
06.10.2015 / 22:25