I am struggling with a double grouping using C#/LINQ on data similar in shape to the following example. I’m starting with a list of TickItems coming from the data layer, and I have now got it shaped as such:
TickItem {
ItemName: string;
QtyNeeded: int;
QtyFulfilled: int;
Category: string;
}
List<TickItem> items = new List<TickItem>();
items.Add("apple", 1, 0, "food");
items.Add("orange", 1, 1, "food");
items.Add("orange", 1, 0, "food");
items.Add("bicycle", 1, 1, "vehicle");
items.Add("apple", 1, 1, "food");
items.Add("apple", 1, 0, "food");
items.Add("car", 1, 1, "vehicle");
items.Add("truck", 1, 0, "vehicle");
items.Add("banana", 1, 0, "food");
I need to group this data by Category, with the sum of each numeric column in the end result. In the end, it should be shaped like this:
{ "food": { "apple" : 3, 1 },
{ "banana" : 1, 0 },
{ "orange" : 2, 1 } },
{ "vehicle": { "bicycle": 1, 1 },
{ "car" : 1, 1 },
{ "truck" : 1, 0} }
I have been able to do each of the groupings individually (group by ItemName and group by Category), but I have not been able to perform both groupings in a single LINQ statement. My code so far:
var groupListItemName = things.GroupBy(tiλ => tiλ.ItemName).ToList();
var groupListCategory = things.GroupBy(tiλ => tiλ.Category).ToList();
Can anyone help?
[edit: I can use either method or query syntax, whichever is easier to visualize the process with]
This query will return sequence of anonymous objects representing items grouped by category. Each category object will have list of anonymous objects, which will contain totals for each item name.