10

I have Items from a certain source (populated from somewhere else):

public class ItemsFromSource{
    public ItemsFromSource(string name){
        this.SourceName = name;
        Items = new List<IItem>();
    }

    public string SourceName;
    public List<IItem> Items;
}

Now in MyClass I have Items from several sources (populated from somewhere else):

public class MyClass{
    public MyClass(){
    }

    public List<ItemsFromSource> BunchOfItems;
}

Is there a simple way to iterate through all Items in all ItemsFromSources in BunchOfItems in one go? i.e., something like:

foreach(IItem i in BunchOfItems.AllItems()){
    // do something with i
}

instead of doing

foreach(ItemsFromSource ifs in BunchOffItems){
    foreach(IItem i in ifs){
        //do something with i
    }
}
Dario
  • 48,658
  • 8
  • 97
  • 130
eriksmith200
  • 2,159
  • 5
  • 22
  • 33
  • if ItemsFromSource isA IItem than your first foreach will work, else neither will work. – Woot4Moo Nov 20 '09 at 17:03
  • I think you should have stated the version of .NET you're using, as some people are offering LINQ as an option, not available with all versions of .NET.. – Aviad Ben Dov Nov 20 '09 at 17:13

4 Answers4

18

Well, you can use the linq function SelectMany to flatmap (create child lists and compress them into one) the values:

foreach(var i in BunchOfItems.SelectMany(k => k.Items)) {}
Dario
  • 48,658
  • 8
  • 97
  • 130
5

You can use SelectMany:

foreach(IItem i in BunchOffItems.SelectMany(s => s.Items)){
    // do something with i
}
bruno conde
  • 47,767
  • 15
  • 98
  • 117
4

You could make a function to do that for you.

Enumerable<T> magic(List<List<T>> lists) {
  foreach (List<T> list in lists) {
     foreach (T item in list) {
       yield return item;
     }
  }
}

Then you just do:

List<List<int>> integers = ...;
foreach (int i in magic(integers)) {
  ...
}

Also, I think PowerCollections will have something for that out of the box.

Aviad Ben Dov
  • 6,351
  • 2
  • 34
  • 45
0
    //Used to flatten hierarchical lists
    public static IEnumerable<T> Flatten<T>(this IEnumerable<T> items, Func<T, IEnumerable<T>> childSelector)
    {
        if (items == null) return Enumerable.Empty<T>();
        return items.Concat(items.SelectMany(i => childSelector(i).Flatten(childSelector)));
    }

I think this will work for what you want to do. Cheers.

Justice
  • 141
  • 5