[英]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..... ....有所有视图位置(比如说 100,这些是 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还有一些数据,我们只从另一个请求中收到与我们的值相对应的位置: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:
有 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这里可以是5个或更多级别的父母
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>
当您获得更多
IEnumerable<TreeViewNode>
时,请记住让您的查找字典保持最新
Another thing you could do is set the parent node inside the TreeViewNode
like this:您可以做的另一件事是在
TreeViewNode
中设置父节点,如下所示:
public class TreeViewNode {
...
// set ParentNode by lookup in the Dictionary once
public TreeViewNode ParentNode {get; set;}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.