You have to use 2 methods. First will invoke the second one and only the second will be recursive!
Here is an example by using Entity Framework:
public static int[] GetCategoyWithChildrenIDList(SLOnlineShopEntities dataContext, int categoryID)
{
List<Category> childrenTree = GetCategoryWithChildrenTreeAsList(dataContext, categoryID);
if (childrenTree.Count < 1)
return new int[0];
//
List<int> idList = new List<int>();
//
foreach (Category category in childrenTree)
{
idList.Add(category.ID);
}
//
return idList.ToArray();
}
private static List<Category> GetCategoryWithChildrenTreeAsList(SLOnlineShopEntities dataContext, int categoryID)
{
List<Category> childrenTree = new List<Category>();
Category mainCategory = dataContext.Categories.FirstOrDefault(c => c.ID == categoryID && (c.Inactive == false || c.Inactive == null));
if (mainCategory == null)
return childrenTree;
//
childrenTree.Add(mainCategory);
List<Category> directChildrenList = dataContext.Categories.Where(c => c.ParentID == categoryID && (c.Inactive == false || c.Inactive == null)).ToList();
if (directChildrenList.Count == 0)
return childrenTree;
//
childrenTree.AddRange(directChildrenList);
//
foreach (Category category in directChildrenList)
{
List<Category> tempList = GetCategoryWithChildrenTreeAsList(dataContext, category.ID);
if (tempList.Count > 0)
childrenTree.AddRange(tempList);
}
//
return childrenTree;
}