I\'ve got a form with multiple fields on it (company name, postcode, etc.) which allows a user to search for companies in a database. If the user enters values in more than
You can use Enumerable.Aggregate combined with Expression.AndAlso. Here's a generic version:
Expression> AndAll(
IEnumerable>> expressions) {
if(expressions == null) {
throw new ArgumentNullException("expressions");
}
if(expressions.Count() == 0) {
return t => true;
}
Type delegateType = typeof(Func<,>)
.GetGenericTypeDefinition()
.MakeGenericType(new[] {
typeof(T),
typeof(bool)
}
);
var combined = expressions
.Cast()
.Aggregate((e1, e2) => Expression.AndAlso(e1, e2));
return (Expression>)Expression.Lambda(delegateType, combined);
}
Your current code is never assigning to combined
:
expr => Expression.And(combined, expr);
returns a new Expression
that is the result of bitwise anding combined
and expr
but it does not mutate combined
.