Retrieving and sorting with GetSubTypeRelation()

Posts   
 
    
tahoepete
User
Posts: 13
Joined: 30-Mar-2007
# Posted on: 13-Oct-2007 01:20:51   
  1. I have two tables in a hierarchy, "Sub" and "Super". I have field values for "Super" and want to find the matching record in "Super" and the related record in "Sub". I can use relations and GetSubTypeRelation("SubEntity") in a GetMulti, but once I have my collection with the "Super" entity back, how do I access the fields in the "Sub" Entity? Or am I going about this the wrong way?

RelationCollection relations = new RelationCollection(): relations.Add(SuperEntity.Relations.GetSubTypeRelation("SubEntity")); SuperCollection supers = GetMulti(myPredicate,0,mySort,relations);

  1. I also want to sort the query by a field in the "Sub" table. I add the relation using GetSubTypeRelation("SubEntity"), and then add a SortExpression using fields from the "Sub" table, but this doesn't seem to work. Am I taking the correct approach here also?

RelationCollection relations = new RelacionCollection(): relations.Add(SuperEntity.Relations.GetSubTypeRelation("SubEntity")); SortExpression mySort = new SortExpression(new SortClause(SubEntityFields.SortField,SortOperator.Ascending)); SuperCollection supers = GetMulti(myPredicate,0,mySort,relations);

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 14-Oct-2007 21:52:14   

tahoepete wrote:

  1. I have two tables in a hierarchy, "Sub" and "Super". I have field values for "Super" and want to find the matching record in "Super" and the related record in "Sub". I can use relations and GetSubTypeRelation("SubEntity") in a GetMulti, but once I have my collection with the "Super" entity back, how do I access the fields in the "Sub" Entity? Or am I going about this the wrong way?

Well, yo have two options here:

A. Retrieve SuperCollection with its corresponding filter, then access the related SubEntity casting.

supers.GetMulti(myPredicate);
int xxx = ((SubEntity) mySuperColl[n]).AnySubField;

B. Retrieve SubCollection (if you only have one subtype this should be the best way), then access the Sub fields that include (by inheritance) the Super fields:

subers.GetMulti(myPredicate);
int xxx = subers[n].AnySubField;
int yyy = subers[n].AnySuperField;

tahoepete wrote:

RelationCollection relations = new RelationCollection(): relations.Add(SuperEntity.Relations.GetSubTypeRelation("SubEntity")); SuperCollection supers = GetMulti(myPredicate,0,mySort,relations);

You don't have to add the relation. LLBLGen include the relations in the fetch because exists an inheritance. LLBLGen adds the relations as LEFT JOIN's. If you add the relation manually, that will be added as INNER JOIN.

tahoepete wrote:

  1. I also want to sort the query by a field in the "Sub" table. I add the relation using GetSubTypeRelation("SubEntity"), and then add a SortExpression using fields from the "Sub" table, but this doesn't seem to work. Am I taking the correct approach here also?

RelationCollection relations = new RelacionCollection(): relations.Add(SuperEntity.Relations.GetSubTypeRelation("SubEntity")); SortExpression mySort = new SortExpression(new SortClause(SubEntityFields.SortField,SortOperator.Ascending)); SuperCollection supers = GetMulti(myPredicate,0,mySort,relations);

As your fisrt answer, if you have only one subtype, is better if you fetch the SubCollection. However your above sorting code should works fine. However you don't have to add the relations.

David Elizondo | LLBLGen Support Team