Search code examples
c#linqexpression-treesef-core-2.1

EF Core dynamic filter


I've been working in a Dynamic filter class for EF Core queries using Expression trees, everything looks good, filter is working, I can pass a filter collection and it works, but when I look at the SQL sentence, it is querying the whole table and applying the filter on the resulting collection, here is my class...

   public static class QueryExpressionBuilder
{
    private static readonly MethodInfo ContainsMethod = typeof(string).GetMethod("Contains", new[] { typeof(string) });
    private static readonly MethodInfo StartsWithMethod = typeof(string).GetMethod("StartsWith", new[] { typeof(string) });
    private static readonly MethodInfo EndsWithMethod = typeof(string).GetMethod("EndsWith", new[] { typeof(string) });

    #region DynamicWhere

    /// <summary>Where expression generator.</summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="filters">The filters.</param>
    /// <returns></returns>
    public static Expression<Func<T, bool>> GetExpression<T>(IList<Filter> filters)
    {
        if (filters.Count == 0)
            return null;

        ParameterExpression param = Expression.Parameter(typeof(T), "t");
        Expression exp = null;

        if (filters.Count == 1)
            exp = GetExpression(param, filters[0]);
        else if (filters.Count == 2)
            exp = GetExpression<T>(param, filters[0], filters[1]);
        else
        {
            while (filters.Count > 0)
            {
                var f1 = filters[0];
                var f2 = filters[1];

                if (exp == null)
                    exp = GetExpression<T>(param, filters[0], filters[1]);
                else
                    exp = Expression.AndAlso(exp, GetExpression<T>(param, filters[0], filters[1]));

                filters.Remove(f1);
                filters.Remove(f2);

                if (filters.Count == 1)
                {
                    exp = Expression.AndAlso(exp, GetExpression(param, filters[0]));
                    filters.RemoveAt(0);
                }
            }
        }

        return Expression.Lambda<Func<T, bool>>(exp, param);
    }

    /// <summary>Comparision operator expression generator.</summary>
    /// <param name="param">The parameter.</param>
    /// <param name="filter">The filter.</param>
    /// <returns></returns>
    private static Expression GetExpression(ParameterExpression param, Filter filter)
    {
        MemberExpression member = Expression.Property(param, filter.PropertyName);
        var type = member.Type;
        ConstantExpression constant;
        switch (type.Name)
        {
            case "Int32":
                constant = Expression.Constant(Convert.ToInt32(filter.Value));
                break;
            case "String":
            default:
                constant = Expression.Constant(filter.Value);
                break;
        }

        // ConstantExpression constant = Expression.Constant(filter.Value);

        switch (filter.Operation)
        {
            case Op.Equals:
                return Expression.Equal(member, constant);

            case Op.GreaterThan:
                return Expression.GreaterThan(member, constant);

            case Op.GreaterThanOrEqual:
                return Expression.GreaterThanOrEqual(member, constant);

            case Op.LessThan:
                return Expression.LessThan(member, constant);

            case Op.LessThanOrEqual:
                return Expression.LessThanOrEqual(member, constant);

            case Op.Contains:
                return Expression.Call(member, ContainsMethod, constant);

            case Op.StartsWith:
                return Expression.Call(member, StartsWithMethod, constant);

            case Op.EndsWith:
                return Expression.Call(member, EndsWithMethod, constant);
        }

        return null;
    }

    /// <summary>And logic connector expression generator.</summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="param">The parameter.</param>
    /// <param name="filter1">The filter1.</param>
    /// <param name="filter2">The filter2.</param>
    /// <returns></returns>
    private static BinaryExpression GetExpression<T>(ParameterExpression param, Filter filter1, Filter filter2)
    {
        var bin1 = GetExpression(param, filter1);
        var bin2 = GetExpression(param, filter2);

        return Expression.AndAlso(bin1, bin2);
    }

    #endregion

}

}

To call this class I do something like this :

var whereDeleg = QueryExpressionBuilder.GetExpression<Tax>(filters).Compile();
var myList = _dbContext.MyEntity.Where(whereDeleg).ToList();

The filters parameter i'm passing is a collection of this class :

public class Filter
{
    public string PropertyName { get; set; }
    public Op Operation { get; set; }
    public object Value { get; set; }
}

I'd appreciate any help.


Solution

  • The main problem is not the class, but the way you use it:

    var whereDeleg = QueryExpressionBuilder.GetExpression<Tax>(filters).Compile();
    var myList = _dbContext.MyEntity.Where(whereDeleg).ToList();
    

    You are taking Expression<Func<T, bool>> from your method, but then the Complie() call converts it to Func<T, bool>. So although _dbContext.MyEntity is IQueryable<T>, there is no IQueryable<T> extension method Where taking Func<T, bool> (all they take Expression<Func<T, bool>>). But since IQueryable<T> inherits (hence is a) IEnumerable<T>, the compiler finds and uses the Where extension method for IEnumerable<T> (defined in Enumerable class).

    This makes the Where (and all following methods if any) to execute client side after executing and materializing the query before Where (in your case - the whole table).

    The difference between IQueryable<T> and IEnumerable<T> is covered by Returning IEnumerable<T> vs. IQueryable<T>. All you need is to make sure you always call IQueryable<T> extension methods instead of the IEnumerable<T> methods with the same name and similarly looking arguments by using Expression<Func<...>> instead of Func<...>.

    With all that being said, you should use your method result directly without calling Compile:

    var predicate = QueryExpressionBuilder.GetExpression<Tax>(filters);
    var myList = _dbContext.MyEntity.Where(predicate).ToList();
    

    or just

    var myList = _dbContext.MyEntity.Where(QueryExpressionBuilder.GetExpression<Tax>(filters)).ToList();
    

    Or even better, add the following custom extension method to the QueryExpressionBuilder class:

    public static IQueryable<T> Where<T>(this IQueryable<T> source, IList<Filter> filters)
    {
        var predicate = GetExpression<T>(filters);
        return predicate != null ? source.Where(predicate) : source;
    }
    

    to be able to use simply (and minimize the chance of making mistakes):

    var myList = _dbContext.MyEntity.Where(filters).ToList();
    

    Side note: The main expression builder method implementation is overcomplicated and also destroys the passed input filters list. It can be simplified as follows (which doesn't have the aforementioned defect):

    public static Expression<Func<T, bool>> GetExpression<T>(IEnumerable<Filter> filters)
    {
        var param = Expression.Parameter(typeof(T), "t");
        var body = filters
            .Select(filter => GetExpression(param, filter))
            .DefaultIfEmpty()
            .Aggregate(Expression.AndAlso);
        return body != null ? Expression.Lambda<Func<T, bool>>(body, param) : null;
    }