I'm currently developing an ASP.NET app where I need to display several charts based on data I receive from a database. I receive them as a list of objects, which looks like this for example:
{
{ name: "A", val: 20 },
{ name: "A", val: 10 },
{ name: "B", val: 6 },
{ name: "C", val: 1 },
{ name: "C", val: 20 }
}
I want to transform this list, the target list should look like this:
{ { name: "A", val: 30 } { name: "B", val: 6 }, { name: "C", val: 21 } }
I want all objects with name "A" to be "merged together" into one object, where val now is the sum of all values of the single objects. Currently I'm using a foor loop and a new List, which I loop through evvery time to achieve this, but I think this is very inefficient. There is probably some LINQ option to do this but I haven't found anything. How can I optimize this?
Thanks in advance!