I try to concate two expressions but get error mention in title on Compile method:
Expression<Func<Appointment, bool>> week1 = StartDateIsBetween(lastMonday, nextSunday);
Expression<Func<Appointment, bool>> week2 = EndDateIsBetween(lastMonday, nextSunday);
BinaryExpression weekOr = Expression.Or(week1.Body, week2.Body);
Func<Appointment, bool> week = Expression.Lambda<Func<Appointment, bool>>(weekOr, week1.Parameters.Single()).Compile();
Additional two method to create Expressions:
private Expression<Func<Appointment, bool>> StartDateIsBetween(DateTime beginningDate, DateTime endDate)
{
return a => a.StartDate >= beginningDate && a.StartDate <= endDate;
}
private Expression<Func<Appointment, bool>> EndDateIsBetween(DateTime beginningDate, DateTime endDate)
{
return a => a.EndDate >= beginningDate && a.EndDate <= endDate;
}
Any idea how to fix this error ? I am beginner with expression trees :/
week1
and week2
have different parameters, because they are created separately. The easiest way would be to use ExpressionInvoke
on your existing expressions, with new ExpressionParameter
instance:
var param = Expression.Parameter(typeof(Appointment));
var weekOr = Expression.Or(Expression.Invoke(week1, param), Expression.Invoke(week2, param));
var week = Expression.Lambda<Func<Appointment, bool>>(weekOr, param).Compile();