You can do it with a higher order function:
Func<Node, decimal> 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<Node>()).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<Node>())
.Sum((Func<Node, decimal>)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<Node> 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<Node, decimal> 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...