Reputation: 305
I'm trying to access the last row of each month using LINQ and lambda expressions however I'm not sure how to do it.
I've got as far as a grouping but the issue is I don't think it contains all the data in the row.
var tonerPrinterList = _tonerPrinterRepo.GetTonerPrinterForDevice(printerId, starDate, endDate, color).GroupBy(tp => new {tp.timestamp.Year, tp.timestamp.Month});
The data structure that is produced by GetTonerPrinterForDevice contains more columns than the timestamp for example nominalCoverage and printerID which I need all these columns
Upvotes: 1
Views: 42
Reputation: 106
I believe this should work. You first order by dates, then you group by month, then you select the last of each group.
var tonerPrinterList = _tonerPrinterRepo.GetTonerPrinterForDevice(printerId, starDate, endDate, color)
.OrderBy(tp => tp.timestamp)
.GroupBy(tp => new {tp.timestamp.Year, tp.timestamp.Month})
.Select(group => group.LastOrDefault());
Upvotes: 1