using System; using System.Collections.Generic; using System.Linq; namespace ConsoleApplication7 { class C3 { public C3(int foo) { Foo = foo; } public int Foo { get; set; } } class C2 { public C2(List<C3> bar) { Bar = bar; } public List<C3> Bar { get; set; } } class C1 { public C1(List<C2> baz) { Baz = baz; } public List<C2> Baz { get; set; } } class Program { public void Run() { C2 c2_1 = new C2(new List<C3> {new C3(1), new C3(2), new C3(5)}); C2 c2_2 = new C2(new List<C3> { new C3(-1000), new C3(100) }); C1 c1 = new C1(new List<C2>() { c2_1, c2_2 }); //Answer = -892, but I can't even get the syntax right for it to compile var resultSum = c1.Baz.Select(x => x.Bar).Sum(y => y.Foo); } static void Main(String[] args) { new Program().Run(); } } }
var resultSum = c1.Baz.Select(x => x.Bar).SelectMany(y => y.Foo).Sum(z => z);
var resultSum = c1.Baz.SelectMany(x => // selecting all Bars as one IEnumerable x.Bar).Sum(x => // summing all Foos. x.Foo);
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)