0

I have IEnumerable<GroupResult<T>> where GroupResult<T> is following:

public class GroupResult<T>
{
 public string Key { get; set; }
 public IEnumerable<T> Items { get; set; }
}

So let's say the collection looks like this:

{
Key: "1",
Items: { "A", "B" }
}

{
Key: "1",
Items: { "C", "D" }
}

{
Key: "2",
Items: { "E", "F" }
}

What operation can I perform to get the following:

{
Key: "1",
Items: { "A", "B", "C", "D" }
}

{
Key: "2",
Items: { "E", "F" }
}
n0e
  • 309
  • 3
  • 12

1 Answers1

0

use GroupBy()

var result = intput.GroupBy(x => x.Key)
                   .Select(x => new GroupResult() 
                          { 
                              Key = x.Key, 
                              Items = x.SelectMany(y => y.Items) 
                           });

https://dotnetfiddle.net/p8HklJ

fubo
  • 44,811
  • 17
  • 103
  • 137
  • Unfortunately that does not change anything: https://imgur.com/a/l9PS7EG - ToString as seen in debugger is defined as: Key {Count}. Each GroupResult has 1 item, even those having the same Key. – n0e Dec 12 '18 at 13:24
  • @n0e https://dotnetfiddle.net/p8HklJ – fubo Dec 12 '18 at 13:36