I have a list of invoices and each record consist of a customer ID and an amount. The aim is to produce a list of payments where each payment is unique per customer (there might be multiple invoices per each customer) and sums the related invoices' amounts to a total.
Producing a list of distinct invoices (with respect to the customer ID) is very easy. The things is that I then only have the value of the first invoice's amount and not the sum.
List<Payment> distinct = invoices
.GroupBy(invoice => invoice.CustomerId)
.Select(group => group.First())
.Select(invoice => new Payment
{
CustomerId = invoice.CustomerId,
Total = invoice.Amount
}).ToList();
Is there a smooth LINQ-fu for that or do I need to go foreach on my list?