c# - iterate through objects with Lists of the object -
i have small program should create simple menu. current problem have object should fill main menu point , under menu points. question is, ilist can have second or third ilist , have no idea how interate on n ilists
example:
mainnodeobj: nodedtoid = 1, itemcode = 'a' ilist<nodedto> childmenunodes { mainnodeobj: nodedtoid = 2, itemcode = '2', ilist<nodedto> childmenunodes { mainnodeobj: nodedtoid = 3, itemcode = '3', }
my problem each childnode can have new childnode , each child node create new object...sounds easy dont know how iterate on n new childnodes
methods:
private ienumerable<nodedto> sortednodes(int id) { var nodes = loadmenunodes(id); foreach (menunode menunode in nodes .where(s => s.menuitemid == null && s.parentnodeid == null) .orderby(x => x?.sequence)) { nodedto tmpmenunode = new nodedto(); tmpmenunode.menunodedtoid = menunode.id; tmpmenunode.menuitemcode = menunode.menuitem?.code; tmpmenunode.childmenunodes = loadchildnodes(menunode.childmenunodes).tolist(); yield return tmpmenunode; } } private ienumerable<nodedto> loadchildnodes(menunodelist menunode) { foreach (menunode childnode in menunode) { nodedto tmchildnode = new nodedto(); tmchildnode.menunodedtoid = childnode.id; tmchildnode.menuitemcode = childnode?.menuitem?.code; tmchildnode.childmenunodes = null; yield return tmchildnode; } } public class nodedto { public int nodedtoid { get; set; } public string itemcode { get; set; } public ilist<nodedto> childmenunodes { get; set; } }
can't use resursive function
by placing in loadchildnodes function.
tmpchildnode.childmenunodes = loadchildnodes(childnode.childmenunodes).tolist();
Comments
Post a Comment