Self Referencing Structure - Selecting Level "1"

Posts   
 
    
ianvink
User
Posts: 394
Joined: 15-Dec-2006
# Posted on: 17-Jul-2007 21:20:05   

I have a self referencing Person table.

Level 0 "L0" are the top parents (they have no parents). Level 1 "L1" are children with a parent whose parent has no parent and so on...

Given an entity like this:

Person --PersonPK --ParentFK (links to PersonPK) --Name

I use this SQL to get only the L1 level.

SELECT   L1.PersonPK, L1.ParentFK, L1.name
FROM         tb_people AS L0 INNER JOIN
                      tb_people AS L1 ON L1.ParentFK= L0.PersonPK
WHERE    (L0.ParentFK IS NULL)

Is this possible in LLBL? How?

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 18-Jul-2007 08:50:58   

Try the following:

RelationCollection relations = new RelationCollection();
relations.Add(PersonEntity.Relations.PersonEntityUsingParentFK, "L0");

PredicateExpression filter = new PredicateExpression(PersonFields.ParentFK.SetObjectAlias("L0") == System.DBNull.Value);

PersonCollection.GetMulti(filter, relations);