locked
Problem to create an "Expression.And" lambda expression RRS feed

  • Question

  • Hi,

    this lambda expression work fine:

    var itemParameter = Expression.Parameter(typeof(T), "item");

                var whereExpression = Expression.Lambda<Func<T, bool>>
                    (
                        Expression.Equal(
                            Expression.Property(
                                itemParameter,
                                PrimaryKeyName
                            ),
                            Expression.Constant(a_id)
                        ),
                        new ParameterExpression[] { itemParameter }
                    );

    a simple lambda expression tree with only an "Expression.Equal" expression.

    My problem is that i have to extend this expression with an and clause, and i can't make it to work :(

    here is my code:

    var itemParameter1 = Expression.Parameter(typeof(T), "item");
                var itemParameter2 = Expression.Parameter(typeof(T), "item2");

                var equals1 = Expression.Equal(
                            Expression.Property(
                                itemParameter1,
                                PrimaryKeyNames[0]
                            ),
                            Expression.Constant(a_ids[0])
                        );

                var equals2 = Expression.Equal(
                            Expression.Property(
                                itemParameter2,
                                PrimaryKeyNames[1]
                            ),
                            Expression.Constant(a_ids[1])
                        );

                var andexpression = Expression.And(equals1, equals2);

                var parameters = new ParameterExpression[] { itemParameter1, itemParameter2 };

                var whereExpression = Expression.Lambda<Func<T, bool>>
                    (andexpression, parameters);

    i get the "Incorrect number of parameters supplied for lambda declaration" error when the Lambda constructor is called.
    this is weird, because the equals1, equals2 and andexpression seems well formed to me.

    Any clue?
    Thanks for your help

    • Moved by nobugz Thursday, October 2, 2008 12:50 PM Not a CLR q (Moved from Common Language Runtime to .NET Base Class Library)
    • Edited by Bombinosh Thursday, October 2, 2008 1:42 PM
    Thursday, October 2, 2008 9:54 AM

Answers

  • After understanding the purpose of Expression.Parameter, i was able to patch my code:
    public T GetByIDs(Guid[] a_ids)
    {
    var itemParameter = Expression.Parameter(typeof(T), "item");

    var whereExpression = Expression.Lambda<Func<T, bool>>
    (
    Expression.And(
    Expression.Equal(
    Expression.Property(
    itemParameter,
    PrimaryKeyNames[0]
    ),
    Expression.Constant(a_ids[0])
    ),
    Expression.Equal(
    Expression.Property(
    itemParameter,
    PrimaryKeyNames[1]
    ),
    Expression.Constant(a_ids[1])
    )),
    new ParameterExpression[] { itemParameter }
    );

    return _context.GetTable<T>().Where(whereExpression).Single();
    }
    • Marked as answer by Bombinosh Friday, October 3, 2008 11:54 AM
    Friday, October 3, 2008 11:53 AM