Click here to Skip to main content
15,879,326 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I wrote this method. It is supposed to get me categories in a dictionary with its parent as a key. The categories look like the following

Plant
    Tree
        Apple
        Banana
        Orange
    Bush
        Blueberry
        Blackberry
Car
    Toyota
        Camry
        Prius
        Avalon
    Ford
        Escape
        Mustang
        Focus


My code is only returning one item from each level: Plant > Tree > Apple or Car > toyota > Camry. Its not returning all of the items. Why?

What I have tried:

public static Dictionary<string, Taxon> GetAllTaxonomyChildren(string parentname, Dictionary<string,Taxon> taxnav = null)
 {
     try
     {
         Taxon rootitem = null;
         TaxonomyManager manager = TaxonomyManager.GetManager();
         if (taxnav == null)
         {
             taxnav = new Dictionary<string, Taxon>();
             rootitem = manager.GetTaxonomy<HierarchicalTaxonomy>(TaxonomyManager.CategoriesTaxonomyId).Taxa.Where(t => t.Name == parentname.ToLower() && t.ParentId == Guid.Empty).FirstOrDefault();
         }
         else
         {
             rootitem = manager.GetTaxonomy<HierarchicalTaxonomy>(TaxonomyManager.CategoriesTaxonomyId).Taxa.Where(t => t.Name == parentname.ToLower()).FirstOrDefault();
         }


         if (rootitem != null)
         {
             var children = manager.GetTaxonomy<HierarchicalTaxonomy>(TaxonomyManager.CategoriesTaxonomyId).Taxa.Where(t => t.ParentId == rootitem.Id);
             if (children != null && children.Any())
             {
                 foreach (Taxon t in children)
                 {
                     taxnav.Add(rootitem.UrlName.ToString(), t);
                     taxnav = GetAllTaxonomyChildren(t.Name, taxnav);
                 }
             }
         }
     }
     catch (Exception ex)
     {
         Log.Write(ex, ConfigurationPolicy.ErrorLog);
     }
     return taxnav;
 }
Posted
Updated 13-Feb-21 8:14am
v2

1 solution

How many iterations of this loop
foreach (Taxon t in children)
{
    taxnav.Add(rootitem.UrlName.ToString(), t);
    return GetAllTaxonomyChildren(t.Name, taxnav);
}

will get executed, given the return statement?

You either need a result collection where all iterations keep adding results to, or you could use the yield return scheme, example here: Virtual Collections and Yield Return[^]

:)
 
Share this answer
 
Comments
Lara Fershizzel 13-Feb-21 14:41pm    
I changed it to taxnav = GetAllTaxonomyChildren(t.Name, taxnav); instead of return GetAllTaxonomyChildren but it is still causing the same issue
Luc Pattyn 13-Feb-21 15:30pm    
The change seems necessary and good, but insufficient.

I have some trouble understanding your code, it seems more complex than is needed. I mostly distrust your expression yielding the children. I'm not convinced rootitem is needed at all.

I'm used to enumerate files in a hierarchical file system, and that basically
goes like so (in a C#-like pseudo-code), starting with an empty result:

public static void AddAllFiles(List result, string folder) {
var entries=Directory.GetDirectoryEntries(folder); // both files and folders
foreach(DirectoryEntry de in entries) {
if (de.IsFile) result.Add(de);
else AddAllFiles(result, de.Name); // if is folder
}
}

So there is no special treatment of the first entry, no rootitem.

Suggestion: increase observability, do whatever it takes to see what is actually going on. There are basically two ways to do that:
1. use logging (add print statements documenting the steps (i.e. adding to result, recursing, ...)
2. use the debugger.

For a lot of problems, including this one, I prefer logging, as that results in an organized report of what went on, whereas debugging shows only snapshots (like a frame-by-frame movie).

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900