linq-to-sql combine child expressions
- by VictorS
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 = t => t.start_date >= startDate;
Expression<Child, bool> endDateExpression = t => t.end_date <= endDate;
....
ParameterExpression param = startDateExpression.Parameters[0];
Expression<Func<T, bool>> Combined = Expression.Lambda<Func<Child, bool>>(
Expression.AndAlso(startDateExpression.Body, startDateExpression.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?