Skip to content

Instantly share code, notes, and snippets.

View BabaDorin's full-sized avatar

Dorin Baba BabaDorin

View GitHub Profile
@janvanderhaegen
janvanderhaegen / ExpressionCombiner
Created September 5, 2014 19:59
C# code to combine two lambda expressions
//Shoutout to MBoros on stackoverflow
public static class ExpressionCombiner {
public static Expression<Func<T, bool>> And<T>(this Expression<Func<T, bool>> exp, Expression<Func<T, bool>> newExp)
{
// get the visitor
var visitor = new ParameterUpdateVisitor(newExp.Parameters.First(), exp.Parameters.First());
// replace the parameter in the expression just created
newExp = visitor.Visit(newExp) as Expression<Func<T, bool>>;
// now you can and together the two expressions