linq-to-sql combine child expressions

Posted by VictorS on Stack Overflow See other posts from Stack Overflow or by VictorS
Published on 2010-03-30T22:30:37Z Indexed on 2010/03/30 22:33 UTC
Read the original article Hit count: 386

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?

© Stack Overflow or respective owner

Related posts about linq-to-sql

Related posts about lambda-expressions