Group By with multiple Objects vb.net

Viewed 37

I have a list as following

list1 =  [
   {meal: {id: 1, version: 0}, type: {id: 2, version: 0}, adjCount: 1, systemCount: 0},
   {meal: {id: 1, version: 0}, type: {id: 2, version: 0}, adjCount: 2, systemCount: 0},
   {meal: {id: 2, version: 0}, type: {id: 1, version: 0}, adjCount: 4, systemCount: 0},
   {meal: {id: 2, version: 0}, type: {id: 1, version: 0}, adjCount: 7, systemCount: 0},
]

I'm looking for a result such as it returns both mealId and type id with it's respective totals As

{meal: {id: 1, version: 0}, type: {id: 2, version: 0}, adjCount: 3, systemCount: 0},
{meal: {id: 2, version: 0}, type: {id: 1, version: 0}, adjCount: 11, systemCount: 0}

I tried Using Linq with following code

List.GroupBy(Function(myItem) New MyObject With {.mealTypeId = myItem.Meal.Id, .countTypeId = MyItem.Count.Id}).
Select(Function(g) New With
 {
    .MealTypeId = g.Key.MealTypeId,
    .CountTypeId = g.Key.CountTypeId,
    .AdjustmentCount = g.Sum(Function(i) i.AdjustmentCount),
    .SystemCount = g.Sum(Function(i) i.SystemCount)
 }

but it doesn't return the expected output. Any Help will be greatly appreciated, I very rarely ask questions here so please forgive any mistakes I might have done in describing the issue.

0 Answers
Related