Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert an Expression<Func<T,bool>> to an Expression<Func<T1,bool>> so that T is a member of T1

We have an entity of type T1 which has a member of type T. something like this :

public class T1
{
    public T Member{get;set;}
}

User can use our UI to give us a filter over T and we have translate it to an expression of a function that gets a T and returns bool (Expression<Func<T,bool>>)

I would like to know is it possible to convert this to an expression of a function that gets T1 and returns bool.

Actually I'd like to convert this :

(t=>t.Member1==someValue && t.Member2==someOtherValue);

to this :

(t1=>t1.Member.Member1==someValue && t1.Member.Member2==someOtherValue);
like image 608
Beatles1692 Avatar asked Apr 27 '15 10:04

Beatles1692


2 Answers

You can do it with a few way.

First and simplest: use Expression.Invoke

Expression<Func<T, bool>> exprT = t.Member1==someValue && t.Member2==someOtherValue
ParameterExpression p = Expression.Parameter(typeof(T1));
var expr = Expression.Invoke(expr, Expression.PropertyOrField(p, "Member"));
Expression<Func<T1, bool>> exprT1 = Expression.Lambda<Func<T1, bool>>(expr, p);

but in this case you get not

t1 => (t=>(t.Member1==someValue && t.Member2==someOtherValue))(t1.Member), 

instead of

(t1=>t1.Member.Member1==someValue && t1.Member.Member2==someOtherValue);

For replacing you can use ExpressionVisitor class like

    class V : ExpressionVisitor
    {
        public ParameterExpression Parameter { get; private set; }
        Expression m;
        public V(Type parameterType, string member)
        {
            Parameter = Expression.Parameter(parameterType);
            this.m = Expression.PropertyOrField(Parameter, member);
        }
        protected override Expression VisitParameter(ParameterExpression node)
        {
            if (node.Type == m.Type)
            {
                return m;
            }
            return base.VisitParameter(node);
        }
    }

and use it

var v = new V(typeof(T1), "Member");
var exprT1 = Expression.Lambda<Func<T1, bool>>(v.Visit(exprT.Body), v.Parameter);
like image 79
Grundy Avatar answered Nov 04 '22 17:11

Grundy


Given

public class MyClass
{
    public MyInner Member { get; set; }
}

public class MyInner
{
    public string Member1 { get; set; }
    public string Member2 { get; set; }
}

plus

public static Expression<Func<TOuter, bool>> Replace<TOuter, TInner>(Expression<Func<TInner, bool>> exp, Expression<Func<TOuter, TInner>> outerToInner)
{
    var body2 = new ExpressionReplacer { From = exp.Parameters[0], To = outerToInner.Body }.Visit(exp.Body);
    var lambda2 = Expression.Lambda<Func<TOuter, bool>>(body2, outerToInner.Parameters);
    return lambda2;
}

and

public class ExpressionReplacer : ExpressionVisitor
{
    public Expression From;
    public Expression To;

    protected override Expression VisitParameter(ParameterExpression node)
    {
        if (node == From)
        {
            return base.Visit(To);
        }

        return base.VisitParameter(node);
    }
}

you can

// The initial "data"
string someValue = "Foo";
string someOtherValue = "Bar";
Expression<Func<MyInner, bool>> exp = t => t.Member1 == someValue && t.Member2 == someOtherValue;
Expression<Func<MyClass, MyInner>> outerToInner = u => u.Member;

// The "new" expression
Expression<Func<MyClass, bool>> result = Replace(exp, outerToInner);

The ExpressionReplacer class replaces a parameter of an expression with another expression, while the Replace method uses the ExpressionReplacer and then rebuilds a new expression.

like image 39
xanatos Avatar answered Nov 04 '22 18:11

xanatos