I need to create and combine several expressions for child entity into one to use it on "Any" operator of a parent. Code now looks like this:
Expresion<Child, bool> startDateExpression;
if(String.IsNullOrEmpty(startDate)
startDateExpression = t => true;
else
startDateExpression = t => t.start_date >= startDate;
Expression<Child, bool> endDateExpression;
if(String.IsNullOrEmpty(endDate)
endDateExpression = t => true;
else
endDateExpression = t => t => t.end_date <= endDate;
....
ParameterExpression param = startDateExpression.Parameters[0];
Expression<Func<T, bool>> Combined = Expression.Lambda<Func<Child, bool>>(
Expression.AndAlso(startDateExpression.Body, endDateExpression.Body), param);
//but now I am trying to use combined expression on parent
//this line fails just to give an idea on what I am trying to do:
//filter type is IQueryable<Parent>;
var filter = filter.Where(p =>p.Children.Any(Combined));
How can I do that? Is there better(more elegant way way of doing it? Maybe I should convert child expression into parent expression?