Self-Joined Table

Posts   
 
    
KyleJ
User
Posts: 4
Joined: 14-Mar-2005
# Posted on: 15-May-2006 20:41:04   

I'm not sure I know how to ask this......

Here's the example:

"Item" Table:

ID int (primary key) Description nvarchar 200 ParentID int

Relationships:

Item.ParentID -> Item.ID

Data:

ID Description Parent 1 Item 1 3 Item 3 4 Item 2.1 8 5 Item 2.2 8 6 Item 2.1.1 4 7 Item 2.1.2 4 8 Item 2

Is there a way to retrieve an entity and have all of it's related items included and all of their related items included ?

For example if I select ID=8, I would get 8, 4, 6, 7, 5 in a hierarchial structure.

bclubb
User
Posts: 934
Joined: 12-Feb-2004
# Posted on: 16-May-2006 03:23:28   

You can do this using prefetch paths. Since you will have something like item.ParentItem you could do

IPrefetchPath path = new PrefetchPath((int)ItemEntity); path.Add(ItemEntity.PrefetchPathParentItem).SubPath.Add( ItemEntity.PrefetchPathParentItem).SubPath.Add( ItemEntity.PrefetchPathParentItem).SubPath.Add( ItemEntity.PrefetchPathParentItem).SubPath.Add( ItemEntity.PrefetchPathParentItem).SubPath.Add( ItemEntity.PrefetchPathParentItem);

This would fetch all the items and all of the parents.