Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to merge two C# Lambda Expressions without an invoke?

I'd like to merge the following Expressions:

// example class
class Order
{
    List<OrderLine> Lines       
}
class OrderLine { }

Expression<Func<Order, List<OrderLine>>> selectOrderLines = o => o.Lines;
Expression<Func<List<OrderLine>, Boolean>> validateOrderLines = lines => lines.Count > 0;

// now combine those to
Expression<Func<Order, Boolean>> validateOrder;

I got it to work using a invoke on the selectOrderLines and supplying the result to the validateOrderLines, but because I'm using these expressions in Entity Framework, I have to actually create a clean expression which should represent:

Expression<Func<Order, Boolean>> validateOrder = o => o.Lines.Count > 0;

How can I do this?

like image 367
Davy Landman Avatar asked Jul 15 '09 14:07

Davy Landman


People also ask

Can you have multiple C files?

A large C or C++ program should be divided into multiple files. This makes each file short enough to conveniently edit, print, etc. It also allows some of the code, e.g. utility functions such as linked list handlers or array allocation code, to be shared with other programs.

What is merging array in C?

CServer Side ProgrammingProgramming. Take two arrays as input and try to merge or concatenate two arrays and store the result in third array. The logic to merge two arrays is given below − J=0,k=0 for(i=0;i<o;i++) {// merging two arrays if(a[j]<=b[k]){ c[i]=a[j]; j++; } else { c[i]=b[k]; k++; } }


1 Answers

The most elegant way is to use an Expression Visitor. In particular, this MSDN Blog Entry describes how to use it to combine predicates (using boolean And or Or) without Invoke.

EDITED Having realized boolean combination is not what you wanted, I wrote a sample usage of ExpressionVisitor that solves for your particular problem:

public class ParameterToMemberExpressionRebinder : ExpressionVisitor
{
    ParameterExpression _paramExpr;
    MemberExpression _memberExpr;

    ParameterToMemberExpressionRebinder(ParameterExpression paramExpr, MemberExpression memberExpr) 
    {
        _paramExpr = paramExpr;
        _memberExpr = memberExpr;
    }

    protected override Expression Visit(Expression p)
    {
        return base.Visit(p == _paramExpr ? _memberExpr : p);
    }

    public static Expression<Func<T, bool>> CombinePropertySelectorWithPredicate<T, T2>(
        Expression<Func<T, T2>> propertySelector,
        Expression<Func<T2, bool>> propertyPredicate)
    {
        var memberExpression = propertySelector.Body as MemberExpression;

        if (memberExpression == null)
        {
            throw new ArgumentException("propertySelector");
        }

        var expr = Expression.Lambda<Func<T, bool>>(propertyPredicate.Body, propertySelector.Parameters);
        var rebinder = new ParameterToMemberExpressionRebinder(propertyPredicate.Parameters[0], memberExpression);
        expr = (Expression<Func<T, bool>>)rebinder.Visit(expr);

        return expr;
    }

    class OrderLine
    {
    }

    class Order
    {
        public List<OrderLine> Lines;
    }

    static void test()
    {
        Expression<Func<Order, List<OrderLine>>> selectOrderLines = o => o.Lines;
        Expression<Func<List<OrderLine>, Boolean>> validateOrderLines = lines => lines.Count > 0;
        var validateOrder = ParameterToMemberExpressionRebinder.CombinePropertySelectorWithPredicate(selectOrderLines, validateOrderLines);

        // validateOrder: {o => (o.Lines.Count > 0)}
    }
}
like image 127
Ben M Avatar answered Sep 22 '22 08:09

Ben M