Suppose that I have some C# code that looks like this:
var query1 = query.Where(x => x.BirthDate > now);
var query2 = query.Where(x => x.EnrollmentDate > now);
var query3 = query.Where(x => x.GraduationDate > now);
The actual code is more complicated, but I'm using a simple example. I'm passing this code to Entity Framework.
Suppose I look at this and say, "This is un-DRY", and then I write a function like this.
public IQueryable<Student> FilterAfterDate(IQueryable<Student> query,
Expression<Func<Student, DateTime>> GetDateExpression, DateTime now)
{
return query.Where(x => GetDateExpression(x) > now);
}
At runtime, this produces the following error:
The LINQ expression node type 'Invoke' is not supported in LINQ to Entities.
I don't know the particulars, but I believe that the solution to this probably is to somehow take my FilterAfterDate
which is Expression<Func<Student, DateTime>>
and somehow combine that with the datetime comparison to produce an expression of type Expression<Func<Student, bool>>
to pass to the Where
function, but I don't know how to do this.