LINQ is elegant

Sometimes I just love LINQ. I had to traverse an object tree today and I wanted something simple to enumerate every object in the tree into a dictionary. Here is the recursive function I wrote:

public IEnumerable<TestDataItem> GetAllSubItems() {   
  return Items.Aggregate(Items.AsEnumerable(),(list, item) => list.Union(item.GetAllSubItems()));   
}

Calling that on any item in the tree will return all of that items sub items. Then it is a simple matter of calling ToDictionary() on the return value.

comments powered by Disqus