Sorting for the initial retrieval of a collection

Posts   
 
    
ww
User
Posts: 83
Joined: 01-Oct-2004
# Posted on: 07-Oct-2004 17:03:11   

I have an entity, ParentEntity, which has a collection of ChildEntity objects, exposed through property Children. When I access the children through the parent, I want them sorted in a particular order. I used to use code like this:


dim p as new ParentEntity
dim sort as new SortExpression
   '...
   'create the appropriate sort expression
   '...
   p.Children.SortClauses=sort
   p.FetchUsingPK(...)


Then p.Children would be sorted in the proper order.

Since I upgraded to the latest libraries and designer, this no longer works. When I do


p.Children.SortClauses=sort

the code attempts to fetch p.Children. Since p's primary key isn't set yet, it doesn't come back with anything, but the collection is still marked as fetched (_alreadyFetchedChildren=True).

Once I fetch p and then try to use p.Children, there's nothing in the collection, because the fetch isn't done again.

So what's the proper way to set the sort order for a relation, so the items are retrieved in the desired order? Am I supposed to do p.Children.GetMulti(...) after I've fetched p? It seems there needs to be a way to set the sort order before the initial fetch is attempted; otherwise there's always a redundant database read.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39588
Joined: 17-Aug-2003
# Posted on: 07-Oct-2004 17:42:10   

use p.SetCollectionParametersChildren(0, sorter);

The other methods like you use, p.Children... will trigger lazy loading.

Frans Bouma | Lead developer LLBLGen Pro
ww
User
Posts: 83
Joined: 01-Oct-2004
# Posted on: 08-Oct-2004 16:24:40   

Thanks--I overlooked that method