I added method that return expression
public Expression<Func<OrderItem, decimal>> GetExpression()
{
return x => x.UnitPrice*x.Quantity;
}
Then I tried
from oi in orderItems
group oiGrouped by ...
into orderItemsGroupedBySomething
select new
{
Key = orderItemsGroupedBySomething.Key,
Revenue = orderItemsGroupedBySomething.Sum(GetExpression())
}
But it doesn't work such as with @LorentzVedeler answer. Because orderItemsGroupedBySomething is of type IGrouping that don't have Sum method with param of type Expression. Therefore I tried
orderItemsGroupedBySomething.AsQueryable.Sum(GetExpression())
But it causes Internal .NET Framework Data Provider error 1025
. The issue was that I invoked method GetExpression() in linq. To solve this problem I put expression to local variable. The result
var expression = GetExpression();
from oi in orderItems
group oiGrouped by ...
into orderItemsGroupedBySomething
select new
{
Key = orderItemsGroupedBySomething.Key,
Revenue = orderItemsGroupedBySomething.AsQueryable.Sum(expression)
}