lucisferre

“There are two ways of constructing a software design: One way is to make it so simple that there are obviously no deficiencies, and the other way is to make it so complicated that there are no obvious deficiencies. The first method is far more difficult. —Sir Charles Antony Richard Hoare”

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:

1
2
3
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