In this question I answered with this Linq. While it does what I was looking for, I am not sure how easy the linq queries are for others to follow. So I am looking for feedback on formating, what comments would be helpful and other alternative approaches to moving through the children records.
private void UpdateGridFromSourceHierarchy(int depth, IEnumerable<SourceFile> list)
{
//Get the initial set of sourcefiles
var sourceFiles = list
.SelectMany(current => current.getInvocations()
.Select(invocation => new {current = (SourceFile) null, invocation}))
.ToList() //This ensures that getInvocations is only called once for each sourcefile
.GroupBy(x => x.current, x => x.invocation);
for (var currentDepth = 0; currentDepth <= depth; currentDepth++)
{
foreach (var currentGroup in sourceFiles)
{
int sourceFileCount = currentGroup.Count();
int counter = 0;
foreach (var invocation in currentGroup)
{
/*
* Generalized grid code goes here
* In my code it was a call to:
* UpdateGridPosition(currentGroup,invocation,counter);
*/
counter++;
}
}
//Select the current sub source files
sourceFiles = sourceFiles.SelectMany(current => current.Select(invocation => invocation))
//Get all of the invocations paired with the new current level of source files
.SelectMany(newCurrent => newCurrent.getInvocations()
.Select(invocation => new { newCurrent, invocation }))
//Group them so that we can loop through each set of invocations seperately
.ToList().GroupBy(x => x.newCurrent, x => x.invocation);
}
}