您可以用高階函數做:
Func<Node, decimal> summer = null;
summer = node => node.Amount +
(node.Children == null ? 0m : node.Children.Sum(summer));
decimal total = summer(amounts);
需要注意的是,如果你能保證node.Children永遠不會爲空,夏可簡單了:
summer = node => node.Amount + node.Children.Sum(summer);
另外,你可以使用空合併運算符:
summer = node => node.Amount +
(node.Children ?? Enumerable.Empty<Node>()).Sum(summer);
當然,你可以把它放在一個單獨的方法改爲:
static decimal SumNodes(Node node)
{
return node.Amount +
(node.Children ?? Enumerable.Empty<Node>())
.Sum((Func<Node, decimal>)SumNodes);
}
請注意,這裏的醜是由於方法組轉換中的含糊不清造成的。方法組在類型推斷中沒有太多的愛。
然後致電SumNodes(amount)
。
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);
}
}
我不知道我會打電話給任何這些「簡單」的LINQ查詢,請注意...
喬恩,我會再次讚賞這一次,但不會讓我。 – 2009-06-09 10:05:20