-1

I'm pretty new to C# & Linq. Let's say we have this array of objects:

[
   { id: 1, content_id: 1 },
   { id: 1, content_id: 2 },
   { id: 1, content_id: 3 },
   { id: 2, content_id: 1 },
   { id: 5, content_id: 5 }
]

and I want to group them together to create an array of ints. It should be like:

[
   { id: 1, content_ids: [1, 2, 3] },
   { id: 2, content_ids: [1] },
   { id: 5, content_ids: [5] }
]

How can I do that in C# with Linq? I can loop the List and create a new object one by one, but I believe Linq must have a better and more concise way to do it.

Thanks!

DiplomacyNotWar
  • 31,605
  • 6
  • 57
  • 75
BloodyKitten
  • 17
  • 1
  • 2

0 Answers0