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
.