1
hier ein Demo Model:Wie kann ich Gruppe über mehrere Spalten in einer Hierarchie mit LINQ
public class Foo
{
public DateTime SomeDate { get; set; }
public int SomeValue { get; set; }
}
Und der Code habe ich bisher:
//Some sample data
var fooList = new List<Foo> {
new Foo { SomeDate = DateTime.Now.AddMonths(0), SomeValue = 1 },
new Foo { SomeDate = DateTime.Now.AddMonths(0), SomeValue = 2 },
new Foo { SomeDate = DateTime.Now.AddMonths(6), SomeValue = 3 },
new Foo { SomeDate = DateTime.Now.AddMonths(6), SomeValue = 4 },
new Foo { SomeDate = DateTime.Now.AddMonths(12), SomeValue = 5 },
new Foo { SomeDate = DateTime.Now.AddMonths(12), SomeValue = 6 },
new Foo { SomeDate = DateTime.Now.AddMonths(14), SomeValue = 7 },
new Foo { SomeDate = DateTime.Now.AddMonths(14), SomeValue = 8 }
};
//The query
var result = from foo in fooList
group foo by new { foo.SomeDate.Year, foo.SomeDate.Month } into g
select new
{
//This should be the parent
TheYear = g.Key.Year,
//This should be the content
TheMonth = g.Key.Month,
TheSum = g.Sum(e=>e.SomeValue)
};
Das bin ich so etwas wie dieses gibt:
[0] = { TheYear = 2016, TheMonth = 3, TheSum = 3 }
[1] = { TheYear = 2016, TheMonth = 9, TheSum = 7 }
Was ich zu tun versuchen, ist jedes Jahr kombiniert bekommen, so habe ich eine Liste der Jahre, die so etwas wie diese enthalten:
[0] = {
TheYear = 2016
TheContent = {
[0] = { TheMonth = 3, TheSum = 3 },
[1] = { TheMonth = 9, TheSum = 7 },
}
}
Wie kann ich das erreichen?