c# - Creating a linear datatable where data each pair of consecutive columns represents parent- child relation -
i have datatable in c# following structure.
id name parentid 1 0 2 b 1 3 c 1 4 d 3 5 e 3 6 f 2 7 g 2 now want create datatable follows
parent child1 child2 child3 .... b f b g c d c e any suggestions ??
any suggestions ?
in end, there may clever linq query uses recursive function, think how solve logically , write out in code:
find of "root" items (items don't have valid parent)
var roots = dt.where(r => dr["parentid"] == 0);for each root item, find of items have items id parent
foreach(var root in roots) var children = dt.where(r => dr["parentid"] == root);for each child, find items children of it, etc.
2 , 3 can end being simple 3-4 line recursive function call - how recursive functions work , i'm sure you'll figure out. if not, post when you're stuck.
Comments
Post a Comment