How to return an entity from a 1:n relation

Posts   
 
    
e106199
User
Posts: 175
Joined: 09-Sep-2006
# Posted on: 16-Apr-2009 23:44:03   

Hi i have a table called Staff, and another one called StaffHistory. the primary key for Staff table is StaffID, and primary key for StaffHistory is StaffID and AcademicYearID combined.

How can i return a staff entity that has the fields of the staff table and the fields of the StaffHistory table with AcademicYearID = somevalue ?

thank you -shane

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 17-Apr-2009 03:54:45   

Hi Shane,

There are a lot of ways to do that. For example you could do something like this (for your previous posts I assume you are using SelfServicing):

PredicateExpression filter = new PredicateExpression();
filter.Add(StaffHistoryFields.AcademicYearId = someValue);

StaffHistoryCollection staffHistories = new StaffHistoryCollection();
staffHistories.GetMulti(filter);

// then you can access the entities like this (for instance):
foreach(StaffHistory sh staffHistories)
{
     string someString = sh.SomeFieldOfStaffHistory;
     string anotherString = sh.Staff.SomeFieldOfStaff;
}

If you will need to access a lot of related fields at once you can use PrefetchPaths to improve the performance.

Is that what you are looking for?

David Elizondo | LLBLGen Support Team
e106199
User
Posts: 175
Joined: 09-Sep-2006
# Posted on: 17-Apr-2009 07:35:58   

Hi, for an academicyear, there is only one staffhistory entity. i think creating a staffhistory entity by providing the staffid and academicyearid is the easiest way to go. i was thinking maybe i could create a staff entity that has the matching staffhistory entity that is filtered by the academicyearid but i think it doesnt make a lot of sense. something like staff.staffhistory.somefieldofstaffhistory which will give me the value of somefieldofstaffhistory for the staff entity and selected academic year.

creating a seperate staffhistory entity is a lot easier i guess. thank you -shane

daelmo wrote:

Hi Shane,

There are a lot of ways to do that. For example you could do something like this (for your previous posts I assume you are using SelfServicing):

PredicateExpression filter = new PredicateExpression();
filter.Add(StaffHistoryFields.AcademicYearId = someValue);

StaffHistoryCollection staffHistories = new StaffHistoryCollection();
staffHistories.GetMulti(filter);

// then you can access the entities like this (for instance):
foreach(StaffHistory sh staffHistories)
{
     string someString = sh.SomeFieldOfStaffHistory;
     string anotherString = sh.Staff.SomeFieldOfStaff;
}

If you will need to access a lot of related fields at once you can use PrefetchPaths to improve the performance.

Is that what you are looking for?