I have a data set (cars):
Brand DateSold Amount Bought/Sold
Toyota 06/07/2015 18.5 Bought
BMW 01/01/2016 25.15 Sold
Mercedes 06/06/2016 20.75 Bought
and I want to group by year and return the sum of the amounts i.e.:
Year Amount
2015 -18.5
2016 4.4
and output it into a listbox.
I can sum without the bought/sold condition:
var AmountsByYear = cars.GroupBy(i => i.Date.Year)
.Select(g => new {
Year = g.Key,
Total = g.Sum(i => i.Amount)
}).ToList();
lstAmountsByYear.DataSource = AmountsByYear;