but say you only want to have one clause or both. Well you could make three separate expressions, but what if you wanted to add even more later? What if you wanted to mix and match? What if you’re reading thing because you watched to see what page could possibly be on the last page of a google search? Well I have answers… stolen answers.

First the needed And and Or methods:

publicstaticExpression<Func<T, Boolean>> And<T>(
Expression<Func<T, Boolean>> expressionOne,
Expression<Func<T, Boolean>> expressionTwo
)
{
//Basically this is like a bridge between the two expressions. It will take the T //parameter from expressionOne and apply it to expression two. So if // oneItem => oneItem.OneMethod() is expressionOne // twoItem => twoItem.TwoMethod() is expressionTwo //it would be like replacing the twoItem with the oneItem so that they now point //to the same thing. var invokedSecond = Expression.Invoke(expressionTwo, expressionOne.Parameters.Cast<Expression>());
//Now this is to create the needed expresions to return. It will take both early expressions //and use the item from the first expression in both. //It will look something like this: //currentItem => (currentItem.OneMethod And Invoke(currentItem => currentItem.TwoMethod())) //As you can see, it looks to be running expressionOne and then a new method that basically //calls expressionTwo with the same value (currentItem) returnExpression.Lambda<Func<T, Boolean>>(
Expression.And(expressionOne.Body, invokedSecond), expressionOne.Parameters
);
}
publicstaticExpression<Func<T, Boolean>> Or<T>(
Expression<Func<T, Boolean>> expressionOne,
Expression<Func<T, Boolean>> expressionTwo
)
{
var invokedSecond = Expression.Invoke(expressionTwo, expressionOne.Parameters.Cast<Expression>());
returnExpression.Lambda<Func<T, Boolean>>(
Expression.Or(expressionOne.Body, invokedSecond), expressionOne.Parameters
);
}