How to build a LambdaExpression from an existing LambdaExpression Without Compiling
Asked Answered
P

1

5

I want to combine two LambdaExpressions without compiling them.

This is what it looks like if I do compile them:

    public Expression<Func<TContainer,bool>> CreatePredicate<TContainer,TMember>(
        Expression<Func<TContainer,TMember>> getMemberExpression, 
        Expression<Func<TMember,bool>> memberPredicateExpression)
    {
        return x => memberPredicateExpression.Compile()(getMemberExpression.Compile()(x));
    }

That's obviously not the fastest way to get the target expression from the provided arguments. Also, it makes it incompatible with query providers like LINQ to SQL that do not support C# method calls.

From what I've read it seems like the best approach is to build an ExpressionVisitor class. However, this seems like it could be a pretty common task. Does anyone know of an existing open source code base that provides this kind of functionality? If not, what is the best way to approach the ExpressionVisitor to make it as generic as possible?

Propagate answered 19/3, 2011 at 12:7 Comment(0)
D
4

I don't know if it's the best way, but you could do something like that:

public Expression<Func<TContainer,bool>> CreatePredicate<TContainer,TMember>(
    Expression<Func<TContainer,TMember>> getMemberExpression, 
    Expression<Func<TMember,bool>> memberPredicateExpression)
{
    ParameterExpression x = Expression.Parameter(typeof(TContainer), "x");
    return Expression.Lambda<Func<TContainer, bool>>(
        Expression.Invoke(
            memberPredicateExpression,
            Expression.Invoke(
                getMemberExpression,
                x)),
        x);
}

Usage:

var expr = CreatePredicate(
    (Foo f) => f.Bar,
    bar => bar % 2 == 0);

Result:

x => Invoke(bar => ((bar % 2) == 0), Invoke(f => f.Bar, x))

I guess it would be better to get something like x => x.Bar % 2 == 0, but it would probably be significantly harder...


EDIT: actually it wasn't so hard with an expression visitor:

public Expression<Func<TContainer,bool>> CreatePredicate<TContainer,TMember>(
    Expression<Func<TContainer,TMember>> getMemberExpression, 
    Expression<Func<TMember,bool>> memberPredicateExpression)
{
    return CombineExpressionVisitor.Combine(
        getMemberExpression,
        memberPredicateExpression);
}

class CombineExpressionVisitor : ExpressionVisitor
{
    private readonly ParameterExpression _parameterToReplace;
    private readonly Expression _replacementExpression;
    private CombineExpressionVisitor(ParameterExpression parameterToReplace, Expression replacementExpression)
    {
        _parameterToReplace = parameterToReplace;
        _replacementExpression = replacementExpression;
    }

    public static Expression<Func<TSource, TResult>> Combine<TSource, TMember, TResult>(
        Expression<Func<TSource, TMember>> memberSelector,
        Expression<Func<TMember, TResult>> resultSelector)
    {
         var visitor = new CombineExpressionVisitor(
            resultSelector.Parameters[0],
            memberSelector.Body);
        return Expression.Lambda<Func<TSource, TResult>>(
            visitor.Visit(resultSelector.Body),
            memberSelector.Parameters);
    }

    protected override Expression VisitParameter(ParameterExpression parameter)
    {
        if (parameter == _parameterToReplace)
            return _replacementExpression;
        return base.VisitParameter(parameter);
    }
}

It gives the following expression:

f => ((f.Bar % 2) == 0)
Damalus answered 19/3, 2011 at 12:42 Comment(5)
Thanks - I'm actually going for what you said was 'significantly harder'. This might end up being a good starting point though.Propagate
@Thomas, Okay, this is what I'm thinking: Do what you suggested, but first compare the ParameterExpressions of the LambdaExpression to make sure that there aren't duplicate parameter names. Then, have an ExpressionVisitor visit ExpressionType.Invoke, selecting the arguments, and create a new getMemberExpression.Body, with the argument values replacing each parameter occurrence. Then return the LambdaExpression's body. How does this sound to you?Propagate
@smartcaveman, I eventually wrote an ExpressionVisitor solution, which doesn't need Invoke at all. See my updated answerDamalus
@Thomas, in writing this do you see any potential limitations to its use?Propagate
@smartcaveman, well, as it is now, it only support one parameter... but it could probably be improved to handle several (you would need several overloads of the static Combine method of course)Damalus

© 2022 - 2024 — McMap. All rights reserved.