SortExpression with field from related table

Posts   
 
    
aure303
User
Posts: 6
Joined: 06-Jun-2007
# Posted on: 08-Mar-2009 06:21:29   

Hello,

I'm using the self-servicing code.

I'm trying to get some ArticleComments (from ArticleComment table) and sort them by their creators' name (from Member table) like so:

ArticleCommentCollection comments = new ArticleCommentCollection(); IPredicateExpression predicateExpression = new PredicateExpression(); predicateExpression.Add(ArticleCommentFields.IsActive == true); SortExpression sorter = new SortExpression(); sorter.Add(ArticleCommentFields.CreatorId); comments.GetMulti(predicateExpression, 0, sorter, null, pageNumber, pageSize);

This sorts by creator's id and not name. I read a few threads but couldnt get find the answer cry

Anyone could help me?

Thanks a lot in advance!

Aure

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 08-Mar-2009 21:04:55   

Hi Aure. You should use Multi-entity filters. The code in the manual is for filtering but the same applies for sorting. In you code (above) just add the corresponding relation and in the filter include the related entity. For example (approximated code):

ArticleCommentCollection comments = new ArticleCommentCollection();

// the filter
IPredicateExpression predicateExpression = new PredicateExpression();
predicateExpression.Add(ArticleCommentFields.IsActive == true);

// the sorter
SortExpression sorter = new SortExpression();
sorter.Add(ArticleCommentFields.CreatorId);

RelationCollection relationsToUse = new RelationCollection();
relationsToUse.Add(ArticleCommentEntity.Relations.MemeberEntityUsingMemberId);

// fetch
comments.GetMulti(predicateExpression, 0, sorter, relationsToUse, pageNumber, pageSize);

HTH simple_smile

David Elizondo | LLBLGen Support Team
aure303
User
Posts: 6
Joined: 06-Jun-2007
# Posted on: 09-Mar-2009 09:09:28   

Hello Daelmo,

Thanks a lot! It's working good now.

Here's the working code for anyone interested:

ArticleCommentCollection comments = new ArticleCommentCollection();

// the filter IPredicateExpression predicateExpression = new PredicateExpression(); predicateExpression.Add(ArticleCommentFields.IsActive == true);

// the sorter SortExpression sorter = new SortExpression(); sorter.Add(MemberFields.Username | SortOperator.Ascending);

RelationCollection relationsToUse = new RelationCollection(); relationsToUse.Add(ArticleCommentEntity.Relations.MemberEntityUsingMemberId);

// fetch comments.GetMulti(predicateExpression, 0, sorter, relationsToUse, pageNumber, pageSize);

The only thing I had to change from your code was the sort on the MemberFields.Username.

Thank you again.

Aure