I want to group list of my objects and sum their totals so basically at the end results might looks like this:
1 Coca cola 3x 15.00$
2 Pepsi 1x 5.00$
instead of this:
1 Coca Cola 2x 10.00$
2 Pepsi 1x 5.00$
3 Coca Cola 1x 5.00$
Here is my code:
List<Product> result = productsList
.GroupBy(l => l.Article.Title)
.Select(cl => new Product
{
// Avoid assigning all the properties somehow?
TotalAmount = cl.Sum(c => c.TotalAmount),
}).ToList();
My Product
class has like 20 properties and I would like to avoid populating all of them but I want to keep their values as they are (I dont want to lose values of other properties).
So is this possible to achieve if I want to group products and change their TotalAmount
as I want to sum them and that's it.
Is this possible somehow?