Sort on SubPath

Posts   
 
    
jflegere
User
Posts: 33
Joined: 22-Jul-2005
# Posted on: 03-Apr-2008 06:11:53   

My application has a HouseholdEntity which holds Name ("Smith, John & Jane"), Address, Phone, etc. I have a ClientEntity which has a 1:1 relationship with HouseholdEntity. I also have an EmergencyContactEntity which has a 1:1 relationship with HouseholdEntity. The ClientEntity has a 1:n relationship with EmergencyContactEntity.

What I'm trying to get is a list of all the emergency contacts for a client, sorted by name (ClientEntity.EmergencyContact.Household.Name). The code that I'm using is:

emergencyContactListSorter = new SortExpression(HouseholdFields.Name | SortOperator.Ascending);

IPrefetchPathElement2 emergencyContactElement = ClientEntity.PrefetchPathEmergencyContact;

emergencyContactElement.SubPath.Add(EmergencyContactEntity.PrefetchPathHousehold, 0, null, null, emergencyContactListSorter);

prefetchPath.Add(emergencyContactElement);

However, the list is not sorted as expected. Can you tell me what I'm doing wrong?

If I use this code, the emergency contacts list is sorted as expected:

emergencyContactListSorter = new SortExpression(HouseholdFields.Name | SortOperator.Ascending);

IPrefetchPathElement2 emergencyContactElement = ClientEntity.PrefetchPathHouseholdCollectionViaEmergencyContact;

prefetchPath.Add(emergencyContactElement);

But I would rather use the syntax "ClientEntity.EmergencyContact[i].Household" rather than "ClientEntity.HouseholdCollectionViaEmergencyContact[i]".

Thanks for any help,

Jay

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 03-Apr-2008 07:40:25   

Hi Jay...

jflegere wrote:

emergencyContactListSorter = new SortExpression(HouseholdFields.Name | SortOperator.Ascending);

IPrefetchPathElement2 emergencyContactElement = ClientEntity.PrefetchPathEmergencyContact;

emergencyContactElement.SubPath.Add(EmergencyContactEntity.PrefetchPathHousehold, 0, null, null, emergencyContactListSorter);

prefetchPath.Add(emergencyContactElement);

Above you are sorting on the EmergencyContactEntity.Household objects. But the EmergencyContactEntity<->Household relation is 1:1 so there is nothing to sort (1 entity per EmergencyContact).

Instead, you need to sort the EmergencyContatEntity based on its related Household entity:

emergencyContactListSorter = new SortExpression(HouseholdFields.Name | SortOperator.Ascending);

IRelationCollection relations = new RelationCollection();
relations.Add(EmergencyContatEntity.Relations.HouseholdEntityUsingHouseholdId);

prefetchPath.Add(ClientEntity.PrefetchPathEmergencyContact, 0, null, relations, emergencyContactListSorter)
     .SubPath.Add( EmergencyContactEntity.PrefetchPathHousehold );
David Elizondo | LLBLGen Support Team
jflegere
User
Posts: 33
Joined: 22-Jul-2005
# Posted on: 03-Apr-2008 12:57:58   

Ahhh... I get it. simple_smile

Thanks David.