Skip to content

Not able to retrieve value from BinaryExpression.Right #2

Description

@jabreuar

I was trying to use the WalkTree method, but it was getting an error: propertyValue.Value is not defined.

I already fixed it. I just built a new method to retrive the value from a BinaryExpression:

private static void WalkTree(BinaryExpression body, ExpressionType linkingType, ref List queryProperties)
{
if (body.NodeType != ExpressionType.AndAlso &&
body.NodeType != ExpressionType.OrElse)
{
string propertyName = GetPropertyName(body);
dynamic propertyValue = GetValue(body);
string opr = GetOperator(body.NodeType);
string link = GetOperator(linkingType);

            queryProperties.Add(new QueryParameter(link, propertyName, propertyValue, opr));
        }
        else
        {
            WalkTree((BinaryExpression)body.Left, body.NodeType, ref queryProperties);
            WalkTree((BinaryExpression)body.Right, body.NodeType, ref queryProperties);
        }
    }

    private static object GetValue(BinaryExpression func)
    {
        return Expression.Lambda(func.Right).Compile().DynamicInvoke();
    }

Btw, very nice implementation 👍

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions