C# 如何在递归迭代对象子对象时仅在找到特定值后添加数据

C# 如何在递归迭代对象子对象时仅在找到特定值后添加数据,c#,.net,recursion,C#,.net,Recursion,我试图将树状分支添加到IDictionary,其中int是对象的深度,如下所示 字典结果示例: 0, Parent 1, Parent.Child 2, Parent.Child.Child 类划分 { 公共int Id{get;set;} public int ParentId{get;set;} 公共分区父{get;set;} 公共ICollection子项{get;set;} } 我使用递归从根父级迭代到所需的子级,同时向字典添加深度和除法。 对于迭代,我使用这个方法

我试图将树状分支添加到
IDictionary
,其中int是对象的深度,如下所示

字典结果示例:

0, Parent
1, Parent.Child
2, Parent.Child.Child

类划分
{
公共int Id{get;set;}
public int ParentId{get;set;}
公共分区父{get;set;}
公共ICollection子项{get;set;}
}
我使用递归从根父级迭代到所需的子级,同时向字典添加深度和除法。 对于迭代,我使用这个方法

       public void GetBranchFromTop(Division division, int selectedNodeId, int selectedNodeDepth, ref IDictionary<int, Division> branch)
        {
            branch.Add(selectedNodeDepth, division);

            if (division.Id == selectedNodeId)
            {               
                return;
            }

            if (division.Children != null)
            {
                foreach (var child in division.Children)
                {
                    selectedNodeDepth = selectedNodeDepth + 1;
                    GetBranchFromTop(child, selectedNodeId, selectedNodeDepth, ref branch);
                }
            }
        }
public void GetBranchFromTop(分区、int-selectedNodeId、int-selectednodededepth、ref-id字典分支)
{
分支机构。添加(已选择节点、分部);
if(division.Id==selectedNodeId)
{               
返回;
}
if(division.Children!=null)
{
foreach(分区中的变量child.Children)
{
selectedNodeDepth=selectedNodeDepth+1;
GetBranchFromTop(子级、selectedNodeId、selectedNodeDepth、ref分支);
}
}
}
当对象只有一个子对象时,此方法可以正常工作。当有更多的孩子加入字典是不可能的,因为深度键是重复的。

我想,只有在找到我正在搜索的对象后,我才需要将对象添加到字典中,但我想不出如何递归地添加对象。

我可以想出两种方法。首先,对于每个树分支,您需要有一个单独的
branch
字典-这意味着每次调用
GetBranchFromTop
,如下所示

public void GetBranchFromTop(
    Division division, 
    int selectedNodeId, 
    int selectedNodeDepth, 
    ref IDictionary<int, Division> foundBranch, 
    IDictionary<int, Division> currentBranch)
{
    if(foundBranch != null){
      return; //no need to continue search if the target branch was found already
    }
    currentBranch.Add(selectedNodeDepth, division);

    if (division.Id == selectedNodeId)
    {               
       foundBranch = currentBranch;
       return;
    }

    if (division.Children != null)
    {
        var nextNodeDepth = selectedNodeDepth + 1;//note this is moved out of the loop
        foreach (var child in division.Children)
        {

            var newBranch = new Dictionary<int, Division>(currentBranch); //copy the branch for each child.
            GetBranchFromTop(child, selectedNodeId, nextNodeDepth, ref foundBranch, newBranch);
      }
   }
}

现在,
parentsAndSelf
列表将包含所需的
Parent->Child->Child.Child
列表,其末尾带有target
Division
。深度将是列表中某个项目的索引。

感谢您的回复!第一种方法工作得很好,但没有找到分支空检查。我正在将
新的IDictionary
传递给方法,所以它总是空的,在这种情况下不为null可能是检查计数的好方法。至于深度计数器,我知道这个错误,但后来决定修复它。第二种方法不适合我,因为我需要深度作为进一步过滤的参数。
public void GetBranchFromTop(
    Division division, 
    int selectedNodeId, 
    int selectedNodeDepth, 
    ref IDictionary<int, Division> foundBranch, 
    IDictionary<int, Division> currentBranch)
{
    if(foundBranch != null){
      return; //no need to continue search if the target branch was found already
    }
    currentBranch.Add(selectedNodeDepth, division);

    if (division.Id == selectedNodeId)
    {               
       foundBranch = currentBranch;
       return;
    }

    if (division.Children != null)
    {
        var nextNodeDepth = selectedNodeDepth + 1;//note this is moved out of the loop
        foreach (var child in division.Children)
        {

            var newBranch = new Dictionary<int, Division>(currentBranch); //copy the branch for each child.
            GetBranchFromTop(child, selectedNodeId, nextNodeDepth, ref foundBranch, newBranch);
      }
   }
}
List<Division> parentsAndSelf = new List<Division>();
Division currentDivision = targetDivision;
while(currentDivision != null){
  parentsAndSelf.Add(currentDivision);
  currentDivision = currentDivision.Parent;
}
parentsAndSelf = parentsAndSelf.Reverse();