score:2

The difference between Func<T, bool> and Func<T1, T2, bool> is that second accept two arguments, so you need to create two parameters with Expression.Parameter.

Code below will create an expression

(t1, t2) => t1.Id == 1 && t2.Name == name;

private static Expression<Func<T1, T2, bool>> CreateExpression<T1, T2>()
{
    var t1 = Expression.Parameter(typeof(T1), "t1");
    var t2 = Expression.Parameter(typeof(T2), "t2");

    var idProp = Expression.PropertyOrField(t1, "Id");
    var nameProp = Expression.PropertyOrField(t2, "Name");

    var body = Expression.AndAlso(
        Expression.Equal(idProp, Expression.Constant(1)),
        Expression.Equal(nameProp, Expression.Constant("name"))
    );

    var lambda = Expression.Lambda<Func<T1, T2, bool>>(body, t1, t2);
    return lambda;
}

Related Articles