我需要为子实体创建多个表达式并将其组合成一个表达式,以便在父实体的“Any”操作符上使用它。代码现在如下所示:
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));
我该怎么做?有更好(更优雅)的方法吗?也许我应该把子表达式转换成父表达式?