I need to dynamically substitute an aggregate function on top of a LINQ to Entities query to perform the operation in database and in the same time to do not hard-code what function do I use, e.g. replace Average()
with Max()
or Min()
in a query like this:
IQueryable<Order> orders = ordersRepository.GetAll();
var q = from o in orders
group o by o.OrderDate into g
select new
{
OrderDate = g.Key,
AggregatedAmount = g.AsQueryable()
.Average(x => x.Amount)
};
I'm stuck to translate Queryable.Average()
into an expression tree that can be put inside the query and would be successfully translated into SQL.