C# 循环,直到在列表中找不到特定数据

C# 循环,直到在列表中找不到特定数据,c#,winforms,C#,Winforms,我有一个list int类,它由child和parentID组成,用于标识 public class id { public int childID {get;set;} public int parentID {get;set;} public string text{get;set;} } 我想要一个循环,基于currentID currentChildID,它有一个parentID e、 g: 在我的列表中,这应该是数据:

我有一个list int类,它由child和parentID组成,用于标识

 public class id
    {
        public int childID {get;set;}
        public int parentID {get;set;}
        public string text{get;set;}
    }
我想要一个循环,基于currentID currentChildID,它有一个parentID

e、 g:

在我的列表中,这应该是数据:

childID --- parentID --- text
  1            0         aaa
  2            1         bbb
  3            2         ccc
  4            2         ddd
假设我的currentID是1,所以我想要得到所有parentID值为1的childID

在其2以上的样本中

然后在获取文本2(即bbb)的内部创建一个条件

然后在此循环之后,验证列表中是否有parentID为2的数据

所以在3和4上面的样本中

然后,如果没有包含数字的parentID值,例如:3和4,循环将结束

所以它是一个连续的循环,直到childID值在列表中没有parentID值

编辑:

到目前为止,除了完成列表中所需的数据外,我还没有尝试过任何与循环相关的方法,但我猜它类似于:

int theCurrentID = 0;
bool initial = idList.Any(n => n.currentID == childID);
if (initial)
{
//loop and gettext
foreach(var x in idList.Where(n => n.currentID == childID))
{

    theCurrentID = x.childID; //supposedly currentID
}

bool next = idList.Any(n => theCurrentID == parentID);
//loop and gettext
if (next)
{
    foreach(var x in idList.Where(n => theCurrentID == parentID)
    {


    }
}
else
{
//parent not found
}

注意:此示例在第二个循环中停止,并且不验证是否仍然存在parentID。

好的,我会尽力帮助您,但我仍然不能100%确定您的期望:

首先将childID重命名为类的ID和名称,因为这非常混乱

public class MyClass
{
    public int ID {get;set;}
    public int parentID {get;set;}
    public string text{get;set;}
}

//loop through all items
foreach(var currentItem in idList.OrderBy(x=>x.ID))
{
  //get a list of parent items 
  List<id> parents = idList.Where(x=>x.parentID == currentItem.ID);

  if(parents.Count == 0)
  {
     //we dont have any records which parentID is current item ID so stop executing loop further
     break;
  }

  //Here you loop through all parents and do what you want
  foreach(var parentItem in parents)
  {
     //Do what you want with parentItem
  }
}

p.s.代码未测试

好的,一种方法是递归函数:

private void Traverse(int childID, List<id> data)
    {
        //Pre-action:: Top to bottom approach, to work on items from top level
        //data.Where(x => x.childID == childID);

        foreach (var child in data.Where(x => x.parentID == childID))
        {
            Traverse(child.childID, data);
        }

        //Post-action:: Bottom to top approach, to work on items from root level
        //data.Where(x => x.childID == childID);
    }
将此功能用作:

int theCurrentID = 1;
List<id> temp = new List<id>();
temp.Add(new id() { childID = 1, parentID = 0, text = "1" });
temp.Add(new id() { childID = 2, parentID = 1, text = "2" });
temp.Add(new id() { childID = 3, parentID = 2, text = "3" });
temp.Add(new id() { childID = 4, parentID = 3, text = "4" });

Traverse(theCurrentID, temp);

我希望这能有所帮助。

另一个“用魔法球预测问题”的问题?到目前为止你尝试了什么?@nvoigt,我已经更新了q。到目前为止,我还没有尝试过任何方法,除了完成列表中所需的数据之外。你至少应该自己尝试一下……尝试10次,当你放弃时,再创建一个问题。这就是Stackoverflow项目的目的,而不是为我创建程序。第一眼看到它,我就知道我需要什么了。谢谢