Having a little trouble constructing a linq statement (C#, query syntax but I could handle method sytax). It needs to support grouping as well as a exposing a calculated property. I have a handle on each individually but can’t get them to seem to work together. Any help would be appreciated.
Data
GrpField Qty Price
------------- --- -----
RED 1 10
RED 2 10
RED 1 50
BLUE 2 30
BLUE 2 50
Output needs to be sum of Total Price (Qty * Price) for each group
Desired Result
RED 80
BLUE 160
This gets me the Total Price line
from x in origdata
let linetotal = x.qty * x.price
select new { x.grpfield, x.qty, x.price, linetotal }
But I am unable to group by the new anonymous type since you can’t run aggregates like sum on anonymous types. If I run the group first, I can’t figure out how to work the linetotal into the mix BEFORE the aggregate sum.
As evidence against your since you can’t run aggregates like sum on anonymous types statement…