I have a list of parent/child IDs and would like to get all child IDs for a given parent ID. There are no null parents (the top level IDs don't appear as child IDs).
Currently the parent/child IDs are recorded as a KeyValuePair in a list, however this could be easily changed to another data structure if that would be better:
List<KeyValuePair<int, int>> groups = new List<KeyValuePair<int, int>>();
groups.Add(new KeyValuePair<int,int>(parentID, childID));
For example, here are sample parent/children. The children of parent 27 would be 5944, 2065, 2066, 2067, 6248, 6249, 6250.
Parent Child
27 1888
1888 5943
1888 5944
5943 2064
5943 2065
5943 2066
5943 2067
2064 6248
2064 6249
2064 6250
Any help would be greatly appreciated!
Why dont you change the type of
Dictionary<int, List<int>>
, where the parent is the key and the value (list of ints) is the children?Then you would get back the list of children using:
You will need to weight out the performance impact as this will speed up reads and slow down writes (a huge majority of the time nobody would even notice).
You will also need to check if the list is in the dictionary using
myitems.TryGet(...)
and if not, you will need to create it, but this is o(1), so is practically instant.Its simple. Just think that you have the list in the following array
You need to use a "Recursive function" to get children dynamically. Just call the following method to get all children of a parent