scottksmith95 / LINQKit

LINQKit is a free set of extensions for LINQ to SQL and Entity Framework power users.
MIT License
1.65k stars 165 forks source link

AggregateBalanced overload for `Expression<Func<Type, bool>>[]`? #190

Closed Bouke closed 1 year ago

Bouke commented 1 year ago

First of all, thank you for this great library. I've been able to rip out quite some (buggy) custom expression builders and replace it with this library. A big help.

I have a list of conditions (Expression<Func<Type, bool>>) that I want to pass to an IQueryable. The number of conditions can be rather large, so I have to resort to AggregateBalanced to prevent a StackOverflowException. However the signatures of AggregateBalanced require an operationToDo of TExpression, in this case: Expression<Func<Type, bool>>. I'd like to pass Expression.Or, but it isn't the proper type:

Expected a method with 'Expression<Func<Type,bool>> Or(Expression<Func<Type,bool>>, Expression<Func<Type,bool>>)' signature

I think with some reflection and/or casting I can probably get this working, but I feel this is a pretty common use-case for AggregateBalanced that would be beneficial to more users.

Example (not working):

var conditions = new Expression<Func<string, bool>>[] {
    a => a == "foo",
    b => b == "bar",
    c => c == "baz",
};
conditions.AggregateBalanced(Expression.Or);

Additionally; instead of taking an array, consider accepting an ICollection<> so that users can pass in a List<> as well.

Bouke commented 1 year ago

And that's where PredicateBuilder comes in:

var conditions = new Expression<Func<string, bool>>[] {
    a => a == "foo",
    b => b == "bar",
    c => c == "baz",
};
conditions.AggregateBalanced(PredicateBuilder.Or);