Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 算法根据idParent属性查找每个子节点_C#_Algorithm_Recursion - Fatal编程技术网

C# 算法根据idParent属性查找每个子节点

C# 算法根据idParent属性查找每个子节点,c#,algorithm,recursion,C#,Algorithm,Recursion,我有一张名为“喝酒”的桌子 下面是它的外观: Name idCategory idParentCategory drink 1 1 alcohol 2 1 nonalcohol 3 1 tea 5 3 juice 4 3 sparkling

我有一张名为“喝酒”的桌子

下面是它的外观:

Name         idCategory  idParentCategory
drink            1              1
alcohol          2              1
nonalcohol       3              1
tea              5              3
juice            4              3
sparkling        6              4
nonsparkling     7              4
pepsi            8              6
schweppes        9              6
wine            10              2
beer            11              2
现在,输入是
idCategory
。如您所见,没有属性
idChildren
。我想做的是找到孩子们的身份证

例如,如果输入为1,则输出应为
1、2、3、4、5、6、7、8、9、10、11

以下是我尝试过的:

public void myMethod()
{
    List<Drinking> drinkingList= (from d in myEntities.Drinking
                              select d).ToList();
    foreach (var d in drinkingList)
    {
       if (d.Drinking2Reference.EntityKey != null)
       {
          s = c.Drinking2Reference.EntityKey.EntityKeyValues[0].Value.ToString();
          idPD = Int32.Parse(s);
          //get idParent
          if (idPC == idCat)
          {
             //if idParent is equal as the input, put this idCategory
             //in a list of integers.
             //Now, here comes the tricky part.
             //I should continue with this loop AND repeat this for
             //every child of idPC.
             //Where to put the call for this method?
             //Where to put the return statement?
             //Here is what I'm doing
             myMethod(idPC);
          }
          else
          {
             myMethod(idPC);
          }
       }

            }
}
public void myMethod()
{
List drinkingList=(从myEntities.Drinking中的d开始)
选择d).ToList();
foreach(drinkingList中的变量d)
{
if(d.Drinking2Reference.EntityKey!=null)
{
s=c.Drinking2Reference.EntityKey.EntityKeyValues[0].Value.ToString();
idPD=Int32.Parse(s);
//得到我的父母
如果(idPC==idCat)
{
//如果idParent与输入相等,则将此idParent放入类别
//在整数列表中。
//现在,棘手的部分来了。
//我应该继续这个循环并重复这个步骤
//idPC的每个孩子。
//将此方法的调用放在何处?
//返回语句放在哪里?
//这就是我正在做的
myMethod(idPC);
}
其他的
{
myMethod(idPC);
}
}
}
}
我的目标是用children和greatchildren的id填写一个列表

public void myMethod(int值)
public void myMethod(int value)
{
    List<int> intList = new List<int>();
    List<Drinking> drinkingList= (from d in myEntities.Drinking
                          select d).ToList();
    foreach (var d in drinkingList)
    {
       if (d.Drinking2Reference.EntityKey != null)
       {
          if(d.idCategory == value)
             intList.Add(value);
          else {
             for(int i=0; i < intList.Count; i++) {
                if(intList.ElementAt(i) == d.idParentCategory)
                   intList.Add(d.idCategory);
             }
          }

       }
    }
    //Print numbers    
}
{ List intList=新列表(); List drinkingList=(从myEntities.Drinking中的d开始) 选择d).ToList(); foreach(drinkingList中的变量d) { if(d.Drinking2Reference.EntityKey!=null) { 如果(d.idCategory==值) intList.Add(值); 否则{ for(inti=0;i

我还没有测试过这个,但我觉得它应该可以工作。如果你发现了什么,请随时发表评论。此外,该表必须按idParentCategory进行排序,才能正常工作。我还要说,在这种情况下,我建议您考虑使用树而不是表。

您的问题不清楚,提供的代码只是一个框架。。请更好地解释你想要达到的目的。@amit我的目标是用孩子的ID填写一个列表。它在foreach崩溃,它说集合被修改了;枚举操作可能无法执行。很抱歉,我忘了您不能在使用该列表的foreach中更改列表。我将foreach更改为常规for循环,现在应该可以工作了。