I am trying to filter the DateTime?
fields of my classes with a generic function. But I got the error "The LINQ expression could not be translated. Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to either AsEnumerable(), AsAsyncEnumerable(), ToList(), or ToListAsync()."
And I dont know what is wrong.
This is my extension function:
public static IQueryable<TEntity>? FilterDateField<TEntity>(this IQueryable<TEntity> entities, Expression<Func<TEntity, DateTime?>> dbField, DateTime dateField)
{
var t1 = entities.Where(e=>dbField.Compile().Invoke(e).HasValue && dbField.Compile().Invoke(e)== dateField);
return t1;
}
I get the answer from another question here: Generic Linq to Entities filter method that accepts filter criteria and properties to be filtered
It was just add this code in the class who holds the
FilterDateField
functionAnd them use de Compose like this: