LINQ recursive query to return hierarchical set of groups

前端 未结 3 1434
花落未央
花落未央 2020-12-09 13:08

Given a list of the following models

public class Team
{
    public int TeamId { get; set; }
    public int ParentTeamId { get; set; }
}

I

3条回答
  •  误落风尘
    2020-12-09 13:37

    So first, your TeamGrouping is actually a bit more complex than it needs to be. All it needs is the Team object and a sequence of itself for children:

    public class TeamNode
    {
        public Team Value { get; set; }
        public IEnumerable Children { get; set; }
    }
    

    Next we'll take our sequence of teams and create a node for each one. Then we'll use ToLookup to group them by their parent ID. (Your use of GroupBy is pretty darn close to this, but ToLookup will be easier.) Finally we can just set each node's children to be the lookup value for that node (note that ILookup will return an empty sequence if the key doesn't exist, so our leaves will be handled perfectly). To finish it off we can return all of the top level nodes by just looking up all nodes with a parent ID of null.

    public static IEnumerable CreateTree(IEnumerable allTeams)
    {
        var allNodes = allTeams.Select(team => new TeamNode() { Value = team })
            .ToList();
        var lookup = allNodes.ToLookup(team => team.Value.ParentTeamId);
        foreach (var node in allNodes)
            node.Children = lookup[node.Value.TeamId];
        return lookup[null];
    }
    

提交回复
热议问题