简体   繁体   中英

How to recursively get all the parents from a child c#?

There are all view locations (let's say 100, these are locations for the TreeView): Id, Name, ParentId 1 Root Null 2 Semi-root 1 3 Semi-semi-root 2 4..... ....

And there is data in which we received only those locations that correspond to our values from another request: Id, Name, ParentId 22 Location1 12 36 Location38 21 99 Location38 3

Need to get all parent hierarchy for data which we get from request. There is the class:

public class TreeViewNode {
  public Guid Id {get; set;}
  public string Name {get; set;}
  public Guid ParentId {get; set;}
}

Test data:

 private IEnumerable<TreeViewNode> SeedData()
        {
            return new List<TreeViewNode>
            {
                new()
                {
                    Id = 1,
                    Name = "Root",
                    ParentId = null
                },
                new()
                {
                    Id = 2,
                    Name = "Semi-root",
                    ParentId = 1
                },
                new()
                {
                    Id = 3,
                    Name = "Semi te",
                    ParentId = 2
                },
                new()
                {
                    Id = 4,
                    Name = "Semi oi",
                    ParentId = 2
                },
                new()
                {
                    Id = 5,
                    Name = "Child",
                    ParentId = 3
                },
                new()
                {
                    Id = 6,
                    Name = "Child 1",
                    ParentId = 4
                },
                new()
                {
                    Id = 7,
                    Name = "Child 2",
                    ParentId = 1
                },
                new()
                {
                    Id = 8,
                    Name = "Child 3",
                    ParentId = 1
                },
                new()
                {
                    Id = 9,
                    Name = "Child 4",
                    ParentId = 1
                },
                new()
                {
                    Id = 10,
                    Name = "Child 6",
                    ParentId = 2
                }
            };
        }

Example data got from request:

 var dataFromRequest = new List<TreeViewNode>
            {
                new()
                {
                    Id = 8,
                    Name = "Child 3",
                    ParentId = 1
                },
                new()
                {
                    Id = 10,
                    Name = "Child 6",
                    ParentId = 2
                },
                new()
                {
                    Id = 33,
                    Name = "Child",
                    ParentId = 3
                },
                new()
                {
                    Id = 4,
                    Name = "Semi oi",
                    ParentId = 2
                }
            };

And as result need to get list of parents like this:

Id Name ParentId
1 Root Null
2 Semi-root 1
3 Semi te 2

And here can be 5 or more levels of parents

Something like this

// create Dictionary for lookups
var lookup = dataFromRequest.ToDictionary(x => x.Id.Value, x => x);

var lookFor = new TreeViewNode()
{
    Id = 6,
    Name = "Child 1",
    ParentId = 4
};

// get all the parents
GetParents(lookup, lookFor);

Helper method to get all parents

private void GetParents(Dictionary<int, TreeViewNode> lookup, 
                                TreeViewNode lookFor)
{
    while (lookFor != null)
    {
        // Alternative: Add lookFor to List<TreeViewNode> here and return at 
        // the end of the method
        Debug.WriteLine($"{lookFor.Id} {lookFor.Name} {lookFor.ParentId}");

        if (lookFor.ParentId == null)
            break;

        // cast ParentId to corrent dataType here Guid or int
        lookup.TryGetValue((int)lookFor.ParentId, out var parentNode);
        lookFor = parentNode;
    }

}

Remember to keep your lookup Dict up to date when you get more IEnumerable<TreeViewNode>

Another thing you could do is set the parent node inside the TreeViewNode like this:

public class TreeViewNode {
    ...
    // set ParentNode by lookup in the Dictionary once
    public TreeViewNode ParentNode {get; set;}
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM