I have a table named Drinking.
Here is how it looks:
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
Now, the input is idCategory. As you can see there isn’t property idChildren. What I am trying to do is find the ids of the children.
For example, if the input is 1, the output should be 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11.
Here is what I tried:
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);
}
}
}
}
My goal is to fill a list with the ids of the children and greatchildren
I haven’t tested this, but I feel like it should work. Feel free to comment if you catch something. Also, the table has to be sorted by idParentCategory for this to work. I will also say that with this situation I would recommend you look into using trees rather than a table.