c# - Iterate throuh list to determine hierarchy level of elements -


there list of items contain field called "hierarchylevel" (type of string) determines hierarchy of elements this: link image. tree structure this:

<ul>  <li>1</li>  <ul>  <li>1.01</li>  <ul>  <li>1.01.01</li>  <li>1.01.02</li>  </ul>  <li>1.02</li>  <ul>  <li>1.02.01</li>  </ul>  <li>1.03</li>  </ul>  <ul>

and on.

my goal implement class contain information parent , children of each element. far have class:

class treenode<dbitem> {     public dbitem value { get; private set; }     public list<treenode<dbitem>> children = new list<treenode<dbitem>>();     public treenode<dbitem> parent { get; private set; }      public string level { get; private set; }      public treenode (dbitem item, string level)     {         this.value = item;         this.level = level;     }      public treenode<dbitem> this[int i]     {         { return this.children[i]; }     }      public treenode<dbitem> addchild(dbitem item, string level)     {         treenode<dbitem> node = new treenode<dbitem>(item, level) { parent = };         this.children.add(node);         return node;     }  } 

the problem don't quite understand how iterate through collection of items. tried this:

treenode<dbitem> parent = new treenode<dbitem>(neededitems[0], "1"); foreach (var doc in neededitems) {     string level = doc.getstringvalue("hierarchylevel");     if (level.startswith("1.")&& level.length < 5)     {         var child1 = parent.addchild(doc, level);         foreach (var child in neededitems)         {             string level1 = child.getstringvalue("hierarchylevel");             if (level1.startswith("1."+level))             {                 child1.addchild(child, level1);             }         }     } } 

but bad approach. , advices on how iterate through list correctly.

we can achieve using:

  • a dictionary of items (to look-up parent nodes)
  • a list of root nodes (in case there more 1 root)
  • a list of dbitem objects ordered hierarchy depth (1 before 1.01)

sample implementation:

class so43271922 {     public so43271922()     {     }      [debuggerdisplay("hierarchylevel = {hierarchylevel}")]     public class dbitem     {         public string name { get; private set; }         public string hierarchylevel { get; private set; }          public dbitem(string name, string hierarchylevel)         {             this.name = name;             this.hierarchylevel = hierarchylevel;         }     }      // dummy list of db item objects     public readonly dbitem[] listitems = new dbitem[] {         new dbitem("element 1", "1"),         new dbitem("element 1.01", "1.01"),         new dbitem("element 1.01.01", "1.01.01"),         new dbitem("element 1.01.02", "1.01.02"),         new dbitem("element 1.02", "1.02"),         new dbitem("element 1.02.01", "1.02.01"),         new dbitem("element 1.03", "1.03")     };      [debuggerdisplay("hierarchylevel = {value.hierarchylevel}")]     public class node     {         public static ireadonlydictionary<string,node> allnodes { { return allnodes; } }         public static ireadonlycollection<node> roots { { return roots; } }          /// <summary>         /// stores references nodex, using hierarchylevel key         /// </summary>         private static dictionary<string, node> allnodes = new dictionary<string, node>();         /// <summary>         /// stores references root nodes         /// </summary>         private static list<node> roots = new list<node>();            public dbitem value { get; private set; }         public node parent { get; private set; }         public list<node> children { get; private set; }          public int level { get; private set; }          public node(dbitem li)         {             this.children = new list<node>();             this.value = li;             allnodes.add(li.hierarchylevel, this);              if (li.hierarchylevel.contains("."))             {                 var parenthier = li.hierarchylevel.substring(0, li.hierarchylevel.lastindexof("."));                 this.parent = allnodes[parenthier];                 this.parent.children.add(this);                 this.level = this.parent.level + 1;             }             else             {                 roots.add(this);                 this.parent = null;                 this.level = 0;             }         }     }      public void generatehierarchy()     {         // sort items by: hierarchy depth, hierarchy level value         var sorteditems = listitems             .orderby(i => i.hierarchylevel.count(c => c == '.')); // 1 before 1.01          foreach (var item in sorteditems)         {             new node(item);         }          var hier = node.roots;     }  } 

hierarchy levels in quickwatch


Comments

Popular posts from this blog

Command prompt result in label. Python 2.7 -

javascript - How do I use URL parameters to change link href on page? -

amazon web services - AWS Route53 Trying To Get Site To Resolve To www -