Using LINQ to group by multiple properties and sum [duplicate]

awavioutt

New Member
This question already has an answer here: I have a collection of items, here it is:\[code\]AgencyID VendorID StateID Amount Fee1 1 1 20.00 5.001 1 1 10.00 2.001 1 1 30.00 8.00 2 2 1 20.00 5.002 2 1 5.00 5.001 1 2 20.00 5.002 2 2 20.00 5.002 2 2 40.00 9.001 2 2 35.00 6.001 2 2 12.00 3.00\[/code\]I'd like these items to be grouped based on the AgencyID, VendorID, and StateID, and the Total calculated from Amount and Fee (Amount + Fee)So using the data above, I'd like to have these results:\[code\]AgencyID VendorID StateID Total1 1 1 75.00 2 2 1 35.001 1 2 25.002 2 2 74.001 2 2 56.00\[/code\]Here's all I have right now, which just gets every row in the database:\[code\]var agencyContracts = _agencyContractsRepository.AgencyContracts. Select(ac => new AgencyContractViewModel { AgencyContractId = ac.AgencyContractID, AgencyId = ac.AgencyID, VendorId = ac.VendorID, RegionId = ac.RegionID, Amount = ac.Amount, Fee = ac.Fee });\[/code\]Does anyone know how I can filter and group this with LINQ?
 
Back
Top