-1

i have the following property:

List<Tuple<string,List<Object>>> GroupedItems

I need a List of all Objects.

Currently I am using a very pragmatic approach with a loop:

          List<Object> flatList = new List<Object>();
          foreach (var y in container.GroupedItems)
          {
            foreach(var z in y.Item2)
            {
            flatList.Add(z);
            }
          }

I am sure this operation can be done in a more comfortable way by using LINQ, unfortunately I do not have much experience with this.

Thanks in advance :)

Bgl86
  • 727
  • 8
  • 20
  • What are `container` and `Item2`? Please see [How to create a Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve). – Chris Pickford Feb 01 '17 at 12:33
  • You don't need to know what is 'container' it is good abstraction. 'Item2' - is obviously some Object cause it's added to such list. – eocron Feb 01 '17 at 12:35
  • 1
    @ChrisPickford `Item2` is the name of the second property in a `Tuple` – René Vogt Feb 01 '17 at 12:35

1 Answers1

3

You may want to use SelectMany:

List<Object> flatList = GroupedItems.SelectMany(item => item.Item2).ToList();

SelectMany projects all items in an enumeration to another enumeration. In your case it projects each tuple to the list of objects in that tuple (Item2).

René Vogt
  • 43,056
  • 14
  • 77
  • 99