c# - Building tree hierarchy using Linq -


i working on creating tree hierarchy using linq , since new facing trouble. have 2 tables have create hierarchy , table follows

table a

id name description 

table b

id of (as foreign key) name  

and need structure this:

name(from table a)      |_name(from table b)      |_name (from table b) name(from table a) 

i have class defined as

  public class c    {         public class c(c item,ienumerable<c> id,ienumerable<c> data)             {                aid=item.ai;               bid=item.bid;                aname=item.name;               childeren=id;             }             public ienumerable<c> children{get;set}          } 

all name of tables must displayed not have nodes

i used group join data both tables , having problem code select node , sub node. linq code follows:

private list<model> buildhierarchy(ienumerable<model> hirs) {     var families=hirs.tolookup(x => x.aid);     var topmost = families.first().select(s => s);     func<string, ilist<model>> children = null;     children = (parentid) => families[parentid]                     .orderby(x => x.bid)                     .select(x => new model(x, children(x.bid), hirs))                     .orderby(o=> o.bid).tolist();      return topmost.select(mod => new model(mod, children(mod.bid), hirs)).tolist(); } 

what gets first node of table node data. when click on node method gets invoked again creating same node.

i have no idea why having problem if guys me great stuck in here long period of time.

thank you

please try this. i'll make edits , make cleaner or more precise.

    dataset ds = new dataset();      datatable dtprimary = new datatable();     dtprimary.columns.add("id", typeof(int));     dtprimary.columns.add("name", typeof(string));       datatable dtforeign = new datatable();     dtforeign.columns.add("id", typeof(int));     dtforeign.columns.add("name",typeof(string));      ds.tables.add(dtprimary);     ds.tables.add(dtforeign);     datarelation dr = new datarelation("myrelation", dtprimary.columns["id"], dtforeign.columns["id"]);      dtprimary.rows.add(1, "name1");     dtprimary.rows.add(2, "name2");      dtforeign.rows.add(1,"child1ofname1");     dtforeign.rows.add(1, "child2ofname1");     dtforeign.rows.add(2, "childofname2");      datatable dtnew = new datatable();     dtnew.columns.add("id");     dtnew.columns.add("name");  var items =     row in dtprimary.asenumerable()     let foreignrow = (     innerrow in dtforeign.asenumerable()     innerrow.field<int>("id") == row.field<int>("id")     select innerrow)     select new { parent = row.field<string>("name"), children = foreignrow }; 

Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -