MyClass consists of ID ParentID and List as Children
I have list of MyClass
Recursion is not necessary here if you build the parent-child relationships before filtering. Since the members of the list remain the same objects, as long as you associate each member of the list with its immediate children, all of the necessary relationships will be built.
This can be done in two lines:
items.ForEach(item => item.Children = items.Where(child => child.ParentID == item.ID)
.ToList());
List topItems = items.Where(item => item.ParentID == 0).ToList();