views:

129

answers:

1

I have a single table with a hierarchy inside. I am using ActiveRecord. How can I convert this into a hierarchic collection that I can then bind to a WinForms or WPF treeview?

Thanks.

ID  Name ParentID
1   ALL 1
2   ARGENTINA 1
15  AUSTRALIA 1
16  NW 15
17  BLACKTOWN 16
18  CORLETTE 16
19  PRIMBEE 16
20  TWEED HEADS 16
21  QL 15
22  ASHMORE 21
23  ROBERTSON 21
24  TOOWOOMBA 21
25  TRINITY BEACH 21
26  UNITED STATES 1
27  AK 26
28  CA 26
29  NJ 26
30  NIKISKI 27
31  AMADOR CITY 28
32  MOSS BEACH 28
33  Essex 29
34  Newark 33
35  Ampere 33
36  Avondale 33
37  Beaufort 33
+3  A: 

Create a partial class and add the property Children (or whatever makes sense).

public IList<MyClass> Children.

Then, in a method somewhere (in your repo, BLL, whatever), loop and load:

var items=MyClass.All().ToList();

items.ForEach(x=>x.Children=items.Where(y=>y.ParentID==x.ID).ToList());
Rob Conery