簡単だ。次の配列にリストがあると考えてください
List<KeyValuePair<int, int>> groups = new List<KeyValuePair<int, int>>();
groups.Add(new KeyValuePair<int, int>(27, 1888));
groups.Add(new KeyValuePair<int, int>(1888, 5943));
groups.Add(new KeyValuePair<int, int>(1888, 5944));
groups.Add(new KeyValuePair<int, int>(5943, 2064));
groups.Add(new KeyValuePair<int, int>(5943, 2065));
groups.Add(new KeyValuePair<int, int>(5943, 2066));
groups.Add(new KeyValuePair<int, int>(5943, 2067));
groups.Add(new KeyValuePair<int, int>(2064, 6248));
groups.Add(new KeyValuePair<int, int>(2064, 6249));
groups.Add(new KeyValuePair<int, int>(2064, 6250));
groups.Add(new KeyValuePair<int, int>(2000, 1000));
// Pass the 1st parameter as the parent to get all children
List<int> childs = GetAllChild(27, groups);
子を動的に取得するには、「再帰関数」を使用する必要があります。次のメソッドを呼び出すだけで、親のすべての子を取得できます
public List<int> GetAllChild(int id,List<KeyValuePair<int, int>> newLst)
{
List<int> list = new List<int>();
for (int i = 0; i < newLst.Count; i++)
{
if (Convert.ToInt32(newLst[i].Key) == id)
{
if (!list.Contains(Convert.ToInt32(newLst[i].Value)))
{
list.Add(Convert.ToInt32(newLst[i].Value));
List<int> l = GetAllChild(newLst[i].Value, newLst);
list.AddRange(l);
}
}
}
return list;
}