I think the code can be simplified using a foreach-loop (or probably directly use the previous foreach-loop). No need to use anonymous function.