.net - How do write LINQ query to do recursion? -


if had object called thing had property id , children children list of thing. this:

public class thing     public property id guid     public property children list(of thing) end class 

now given existing list(of thing), let's called alist sake of example, how use linq recursively loop throw each thing , children find out if item exist in entire hierarchy?

dim alist list(of thing)

in order words, given id, how write linq statement against alist see if id exists anywhere in hierarchy?

hope can me , in advance contribution!

linq isn't designed recursion , doesn't handle well. there ways shoehorn standard recursive function cleaner , easier debug:

public function isintree(thing thething, ienumerable(of thing) tree) boolean     if tree.any(function(t) t.id = thething.id)          isintree = true     elseif children not nothing         isintree = tree.any(function(t) isintree(thething, t.children))     else         isintree = false     end if end function 

Comments

Popular posts from this blog

python - Healpy: From Data to Healpix map -

c - Bitwise operation with (signed) enum value -

xslt - Unnest parent nodes by child node -