这很简单。只需认为您在以下数组中有列表
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;
}