So I have a huge EntityFramework query with EntityFramework, but to keep it simple here a small example:
var amount = 10;
myArticles.GroupBy(p => p.Id)
.Where(grp => grp.Sum(k => k.Amount) > amount
Depending on some parameters I have < amount
or == amount
.
Of course I do not want to always write the whole query again, so I came up with this:
Expression<Func<IGrouping<MyEntity, MyXy>, bool>> whereClause;
and then depending on the input parameters for example:
whereClause = grp => grp.Sum(k => k.Amount) > amount;
myArticles.GroupBy(p => p.Id)
.Where(whereClause);
Now to the question: Is it somehow possible to get make the operator dynamic with Expressions? What I want to write is something like this:
Expression<Func<decimal, int, bool>> operatorExpression = (arg1, arg2) => arg1 == arg2;
whereClause = grp => operatorExpression.Invoke(grp.Sum(k => k.Amount), amount);
I am aware that there is LinqKit, but I really want to know if it is possible to solve this just using Expression Trees and how complicated it is. I do not want to make a Func out of the operatorExpression as the result should be computed on the SQL Server, not in memory.