Sum of hierarchical data using LINQ?

Technically you can write recursive lambda expressions but you need to be insane or insanely bright to try (I haven't figured out which). But you can cheat: Func nodeSum = null; nodeSum = node => { decimal result = node. Amount; if (node.

Children! = null) { result = result + node.Children. Sum(nodeSum); } return result; }; var value = nodeSum(amounts).

Technically you can write recursive lambda expressions, but you need to be insane or insanely bright to try (I haven't figured out which). But you can cheat: Func nodeSum = null; nodeSum = node => { decimal result = node. Amount; if (node.

Children! = null) { result = result + node.Children. Sum(nodeSum); } return result; }; var value = nodeSum(amounts).

You can do it with a higher order function: Func summer = null; summer = node => node. Amount + (node. Children == null?0m : node.Children.

Sum(summer)); decimal total = summer(amounts); Note that if you can ensure that node. Children will never be null, summer can be simpler: summer = node => node. Amount + node.Children.

Sum(summer); Alternatively, you could use the null coalescing operator: summer = node => node. Amount + (node. Children?Enumerable.Empty()).

Sum(summer); Of course you could put this into a separate method instead: static decimal SumNodes(Node node) { return node. Amount + (node. Children?Enumerable.Empty()) .

Sum((Func)SumNodes); } Note the ugliness here is due to an ambiguity in method group conversions. Method groups don't get much love in type inference. And then call SumNodes(amount).

Lots of options :) Full example of the first form: using System; using System.Collections. Generic; using System. Linq; class Node { public decimal Amount; public IEnumerable Children { get; set; } } public class Test { static void Main() { var amounts = new Node { Amount = 10, Children = new { new Node { Amount = 20 }, new Node { Amount = 30 } } }; Func summer = null; summer = node => node.

Amount + (node. Children == null?0m : node.Children. Sum(summer)); decimal total = summer(amounts); Console.

WriteLine(total); } } I'm not sure I'd call any of these a "simple" LINQ query, mind you...

Jon, I'd upvote this a few more times, but SO won't let me. – John Saunders Jun 9 '09 at 10:05 Strictly, that is more of a "higher order function" than a true "recursive lambda"... – Marc Gravell? Jun 9 '09 at 10:07 It wasn't right until I'd fixed it :) – Jon Skeet Jun 9 '09 at 10:07 Added a few more options... – Jon Skeet Jun 9 '09 at 10:11 I get an 'ambiguous invocation' error when I try to implement the SumNodes function.

– Jan Aagaard Jun 9 '09 at 10:52.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions