Search code examples
c#arrays.netlinqsorting

LINQ - How to correctly group a list of users by their nested list of values


I have List<User> with this dependencies:

class User
{
   public string Id { get; set; }
   public List<ManagerGroup> ManagerGroups { get; set; } 
}

class ManagerGroup
{
   public string RoleId { get; set; }
   public Manager[] Managers { get; set; }
}

class Manager
{
   public string Id { get; set; }
}

and want to get the Dictionary value, where key is unique Manager.Id, and value is an array of Users who have this manager. It would be very grateful if you could add sorting by certain ManagerGroup.RoleIds.

Fiddle for tests


Solution

  • This should do what you want:

    var users = new List<User>();
    var usersByManagers = users
        .SelectMany(u => 
            u.ManagerGroups
                .SelectMany(mg => mg.Managers
                    .Select(m => (ManagerId: m.Id, User: u)
                )
            )
        )
        .GroupBy(mu => mu.ManagerId)
        .ToDictionary(g => g.Key, g => g.Distinct().Select(v => v.User).ToArray());
    

    We flatten out the user/manager group/manager arrays into an enumerable of ManagerId/User, group by the manager id, and then build a dictionary with a distinct list of users.

    Documentation:

    Demo (Credit to Yong Shun for the GenerateUsers method).