❔ Sum quantity in a list by name value?
I've joined all of the necessary data from 3 json files into their own object. What I now want to do is take all of the entries in that list and sum the quantity for each of the product names
I'm thinking of separating them into different lists for each product, summing them, and then joining them together in a final array but idk if that's the best route
17 Replies
Can you give a code snippet instead of screenshot?
sure!
Wouldn't a simple linq query serve you?
public static Dictionary<string, int> MostOrdered()
=> Products
.ToDictionary(x => x.ProductName, x => RecentOrders.Count(x => x.ProductName == x.ProductName))
.OrderByDescending(x => x.Value);
Oooh man
This is proof of how much my C# sucks hehe
uhhh
made an error here
That seemed to have made an error
Dw it would have taken me ages to figure that out
public static Dictionary<string, int> MostOrdered()
=> Products
.ToDictionary(x => x.ProductName, x => RecentOrders.Count(y => y.ProductName == x.ProductName))
.OrderByDescending(x => x.Value);
AAAAAAAA
Still an error and idk if this is me being stupid
sorry xD
should be y.Name = x.ProductName
since its Name in RecentOrder class
Nah not that
Cannot implicity cast I think?
I think Linq might be messing with it?
just add another ToDictionary() at the end
I got it
Tysm <3
This also lets me know that I need to learn more about dictionaries
Dictionary is just a collection of KeyValue pairs 🙂
You overestimate how much I know about this language right now xD
Looks like nothing has happened here. I will mark this as stale and this post will be archived until there is new activity.