1

I have this LINQ

var summary =
    from b in books
    group b by b.DateAdded.Value.Year.ToString() into newgroup
        from newgrour2 in
        (
            from b in newgroup
            group b by new
            {
                month = b.DateAdded.Value.ToString("MMMM")
            }
        )
        group new Monthly
        {
            Month = newgrour2.Key.month,
            Count = newgrour2.Count()
        } by new
        {
            Year = newgroup.Key.ToString(),
            Count = newgroup.Count()
        };

which can be fetch by

foreach (var year in Summary)
{
    var y = year.Key.Year;
    var cc = year.Key.Count;
    foreach (var month in year)
    {
        var m = month.Month;
        var c = month.Count;
    }
}

Output will be

2018 - 5 in total
    May - 3 items
    June - 1 item
    July - 1 item
2017 - 4 in total
    February - 4 items

My problem is that I don't want this part to be anonymous

new { Year = newgroup.Key.ToString(), Count = newgroup.Count() };

But if I replace this to

new Yearly { Year = newgroup.Key.ToString(), Count = newgroup.Count() };

It doesn't really group by year; it just goes intp one list.

How do I make the same output but not an anonymous?

if I do this with the anonymous type, summary contains 2 items - 2018 and 2017 which is correct year will now contains its own months But if I do this with Yearly, summary will contain all items as one list. No sub items.

0 Answers0