'Group By, Count and get Total by items inside list

I have an Account object like:

public class Account
{
   public int Id { get; set; }
   public List<Elegibilities> Elegibilities { get; set; }
}

public class Elegibilities
{
   public int Id { get; set; }
   public string Label { get; set; }
   public bool Value { get; set;}
}

My repository returns a List of Account. List<Account>

I want to group by and count by items in Elegibilities and the output would be like:

{
   Total: 30,
    [
       {
          Id : 1,
          Label: "Elegibility1",
          Count: 10
       },
       {
          Id : 2,
          Label: "Elegibility2",
          Count: 20
       }
    ]
}

I'm having issues because it's a list inside another list.

Is it possible to solve in a single LINQ query?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source