递归防止迭代的方法

时间:2016-02-14 13:31:25

标签: c# recursion methods

我有一个sql数据库并输入任何表名,我是cretae table hiyerarchy,如下所示。

Z A B C D E F是表名,Z in是外国ID。 A中有B外国身份证。但是当F表中有D或Z ID名称时。这种递归方法不会停止。

我如何检测到表格中的儿童。

Z
  A
    B
      C=> The continuation of here if comes Z or A or B ID Method not stop and call itself.
  D
    E
    F=> The continuation of here if comes D or Z ID Method not stop and call itself.
     D
     Z

1 个答案:

答案 0 :(得分:0)

以上是上述评论中提出的方法示例...

void RecursiveMethod( Table table, List<string> visitedTables )
{
    // note the currently visited table
    var extendedList = visitedTables.ToList();
    extendedList.Add( table.Name );

    // do your thing with the table...

    // ... and recurse deeper
    foreach (var referencedTable in table.ReferencedTables)
        if (!visitedTables.Contains( referencedTable.Name ))
            RecursiveMethod( referencedTable, extendedList );
}

然后从

开始
RecursiveMethod( theRootTable, new List<string>() );
相关问题