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
function
public static Expression<Func<TFirstParam, TResult>> Compose<TFirstParam, TIntermediate, TResult>(
this Expression<Func<TFirstParam, TIntermediate>> first, Expression<Func<TIntermediate, TResult>> second)
{
var param = Expression.Parameter(typeof(TFirstParam), "param");
var newFirst = first.Body.Replace(first.Parameters[0], param);
var newSecond = second.Body.Replace(second.Parameters[0], newFirst);
return Expression.Lambda<Func<TFirstParam, TResult>>(newSecond, param);
}
public static Expression Replace(this Expression expression, Expression searchEx, Expression replaceEx)
{
return new ReplaceVisitor(searchEx, replaceEx).Visit(expression);
}
internal class ReplaceVisitor : ExpressionVisitor
{
private readonly Expression from, to;
public ReplaceVisitor(Expression from, Expression to)
{
this.from = from;
this.to = to;
}
public override Expression Visit(Expression node)
{
return node == from ? to : base.Visit(node);
}
}
And them use de Compose like this:
public static IQueryable<TEntity>? FilterDateField<TEntity>(this IQueryable<TEntity> entities, Expression<Func<TEntity, DateTime?>> dbField, DateTime dateField)
{
var dateHasValue = dbField.Compose(value => value.HasValue);
var t1 = entities.Where(dateHasValue);
return t1;
}