c# - Child in recursive data table -


i have following table

pnlparentid  id         operator   12           13         * 12           14         * 12           15         *  12           1          - 12           2          -  13           21         / 13           20         / 

i want tree of ids each different operator
how can change following code ? working on hours , appreciated .

var q=  p in typeddatatable       p.parentid == null  // parents      select new        {            parentid = p.parentid,             child =  c in typeddatatable                        c.parentid == p.id select                            new  {childid=c.id,                          parentid = c.parentid}       }; 

i prefer use class storing data (which may have auto-generated if used linq sql):

class typeditem {    public int id {get;set;}    public int parentid {get;set;}    public list<typeditem> children {get;set;}     public typeditem()    {        children = new list<typeditem>();    } } 

then create recursive function populate data:

list<typeditem> getitems(int? parentid) {     var results = p in typeddatatable                   p.parentid == parentid                   select new typeditem(){                       id = p.id,                       parentid = p.parentid                   };      foreach(var result in results)     {         result.children = getitems(result.id);     }      return results.tolist(); } 

which call other code so:

var items = getitems(null); 

note: isn't clear typeddatatable or defined. have assumed globally available, if isn't want pass parameter getitems function.


Comments

Popular posts from this blog

windows - Single EXE to Install Python Standalone Executable for Easy Distribution -

c# - Access objects in UserControl from MainWindow in WPF -

javascript - How to name a jQuery function to make a browser's back button work? -