C
C#2y ago
saki

❔ 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
Tinefol
Tinefol2y ago
Can you give a code snippet instead of screenshot?
saki
sakiOP2y ago
sure!
public class OrderData
{
private static string format = "dd.MM.yyyy";
private static IsoDateTimeConverter converter = new IsoDateTimeConverter { DateTimeFormat = format };

private static string orderJson = File.ReadAllText("data/Orders.json");
private static string productJson = File.ReadAllText("data/Products.json");
private static string joiningJson = File.ReadAllText("data/OrderProducts.json");

private static List<Order> Orders = JsonConvert.DeserializeObject<List<Order>>(orderJson, converter);
private static List<Product> Products = JsonConvert.DeserializeObject<List<Product>>(productJson);
private static List<OrderProduct> Joining = JsonConvert.DeserializeObject<List<OrderProduct>>(joiningJson);

public static List<RecentOrder> RecentOrders = new List<RecentOrder> (from joining in Joining
join orders in Orders on joining.OrderID equals orders.OrderID
join products in Products on joining.ProductID equals products.ProductID
select new RecentOrder(){
ID = orders.OrderID,
Placed = orders.OrderPlaced,
Total = orders.OrderTotal,
Name = products.ProductName,
Quantity = joining.Quantity
}
);

public static List<MostOrdered> MostOrdered = {
// The part I'm trying to figure out
};
}
public class OrderData
{
private static string format = "dd.MM.yyyy";
private static IsoDateTimeConverter converter = new IsoDateTimeConverter { DateTimeFormat = format };

private static string orderJson = File.ReadAllText("data/Orders.json");
private static string productJson = File.ReadAllText("data/Products.json");
private static string joiningJson = File.ReadAllText("data/OrderProducts.json");

private static List<Order> Orders = JsonConvert.DeserializeObject<List<Order>>(orderJson, converter);
private static List<Product> Products = JsonConvert.DeserializeObject<List<Product>>(productJson);
private static List<OrderProduct> Joining = JsonConvert.DeserializeObject<List<OrderProduct>>(joiningJson);

public static List<RecentOrder> RecentOrders = new List<RecentOrder> (from joining in Joining
join orders in Orders on joining.OrderID equals orders.OrderID
join products in Products on joining.ProductID equals products.ProductID
select new RecentOrder(){
ID = orders.OrderID,
Placed = orders.OrderPlaced,
Total = orders.OrderTotal,
Name = products.ProductName,
Quantity = joining.Quantity
}
);

public static List<MostOrdered> MostOrdered = {
// The part I'm trying to figure out
};
}
Tinefol
Tinefol2y ago
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);
saki
sakiOP2y ago
Oooh man This is proof of how much my C# sucks hehe uhhh
Tinefol
Tinefol2y ago
made an error here
saki
sakiOP2y ago
That seemed to have made an error Dw it would have taken me ages to figure that out
Tinefol
Tinefol2y ago
public static Dictionary<string, int> MostOrdered() => Products .ToDictionary(x => x.ProductName, x => RecentOrders.Count(y => y.ProductName == x.ProductName)) .OrderByDescending(x => x.Value);
saki
sakiOP2y ago
AAAAAAAA Still an error and idk if this is me being stupid
Tinefol
Tinefol2y ago
sorry xD should be y.Name = x.ProductName since its Name in RecentOrder class
saki
sakiOP2y ago
Nah not that Cannot implicity cast I think?
saki
sakiOP2y ago
saki
sakiOP2y ago
I think Linq might be messing with it?
Tinefol
Tinefol2y ago
just add another ToDictionary() at the end
saki
sakiOP2y ago
I got it Tysm <3 This also lets me know that I need to learn more about dictionaries
Tinefol
Tinefol2y ago
Dictionary is just a collection of KeyValue pairs 🙂
saki
sakiOP2y ago
You overestimate how much I know about this language right now xD
Accord
Accord2y ago
Looks like nothing has happened here. I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server