I have a complex json structure (at least for me ) that looks like
{ "Assets": [{ "Name": "asset1", "Code": "SS-15", "Items": [{ "Name": "Item1", "KGs": 255, "Cartons": 1222, "Containers": 3 }, { "Name": "Item2", "KGs": 150, "Cartons": 2322, "Containers": 5 } ] }, { "Name": "asset2", "Code": "SA-23", "Items": [{ "Name": "Item1", "KGs": 88, "Cartons": 40, "Containers": 1 }, { "Name": "Item2", "KGs": 960, "Cartons": 710, "Containers": 31 } ] } ]} I need to summarize globally how many KGs, Cartons and Containers are for each type of item, something like this:
[{ "unit": "KGs", "Item1": 343, "Item2": 1110 }, { "unit": "Cartons", "Item1": 1262, "Item2": 3032 }, { "unit": "Containers", "Item1": 4, "Item2": 36 }] I have been using LINQ and so far I have something like:
object.SelectMany(x => x.Items.GroupBy(k => k.Name, m => m.KGs)).GroupBy(g => g.Key); It kind of looks what I am looking for but is not giving me the info I need.
Note: I am deserializing the json to a class in my project.
GroupByinside yourSelectMany, since youGroupByagain afterwards.