I have a method to add a date condition to my linq query. What I want to do is pass x.Due in as a parameter to make this able to work with any date. Any Ideas?
protected virtual IQueryable<TaskView> AddTaskDuePredicate( DateCriteria dateCriterion, IQueryable<TaskView> taskSummary )
{
if ( dateCriterion.Condition == DateCondition.LessThan )
taskSummary = taskSummary.Where( x => x.Due < dateCriterion.Value1 );
else if ( dateCriterion.Condition == DateCondition.LessThanOrEqualTo )
taskSummary = taskSummary.Where( x => x.Due <= dateCriterion.Value1 );
else if ( dateCriterion.Condition == DateCondition.GreaterThan )
taskSummary = taskSummary.Where( x => x.Due > dateCriterion.Value1 );
else if ( dateCriterion.Condition == DateCondition.GreaterThanOrEqualTo )
taskSummary = taskSummary.Where( x => x.Due >= dateCriterion.Value1 );
else if ( dateCriterion.Condition == DateCondition.EqualTo )
taskSummary = taskSummary.Where( x => x.Due == dateCriterion.Value1 );
else if ( dateCriterion.Condition == DateCondition.Between )
taskSummary = taskSummary.Where( x => x.Due <= dateCriterion.Value1 && x.Due >= dateCriterion.Value2 );
return taskSummary;
}
You would have to take the lambda expression as an expression tree like this:
Then you’d have to build an
Expression<Func<TaskView, bool>>from that projection, using things likeExpression.GreaterThanandExpression.Lambda. Off the top of my head:That’s completely untested though. Obviously once you’ve got this working for
GreaterThan, the rest should be relatively easy…