Sort Prefetched Collection on Order Column in join table

Posts   
 
    
Bannis
User
Posts: 4
Joined: 22-Oct-2008
# Posted on: 22-Oct-2008 13:16:53   

Hi

I am using LLBL v2.6, th adapter version.

I have 3 tables, Control, ControlItems and Items

One control can contain many items and items can belong to many controls.

The ControlItems table contains an order column which specifies the order the item will appear in a control, my problem is sorting my prefetched collection on this order value.

My code currently is


 ControlEntity control = new ControlEntity(controlId);
 IPrefetchPath2 prefetchPath = new PrefetchPath2((int)EntityType.ControlEntity);
 prefetchPath.Add(ControlEntity.PrefetchPathItemCollectionViaControlItem);
 using (DataAccessAdapter adapter = new DataAccessAdapter())
 {
    adapter.FetchEntity(control, prefetchPath);
 }

I have tried adding

 SortExpression sorter = new SortExpression(ControlItemFields.Order | SortOperator.Ascending);

to the prefect path like so


prefetchPath.Add(prefetchPath.AddControlEntity.PrefetchPathItemCollectionViaControlItem,0,null,null,sorter);

but I get an error

The multi-part identifier "dbo.ControlItem.order" could not be bound

I understand why I am getting the error, but I don't understand how I get round it?

What is the best way of achieving what I am trying to do?

Thanks

Bannis

Walaa avatar
Walaa
Support Team
Posts: 14993
Joined: 21-Aug-2005
# Posted on: 22-Oct-2008 15:58:52   

I have tried adding SortExpression sorter = new SortExpression(ControlItemFields.Order | SortOperator.Ascending);

to the prefect path like so

prefetchPath.Add(prefetchPath.AddControlEntity.PrefetchPathItemCollectionViaControlItem,0,null,null,sorter);

In the preftechPath you are trying to sort the prefecthedCollection on a field in a related entity. All you have to do is to specify the relation between fetched Item Entity and the ControlItem Entity. This should be passed to the IRelationCollection 4th parameter of the prefetchPath.Add() method.

Bannis
User
Posts: 4
Joined: 22-Oct-2008
# Posted on: 22-Oct-2008 16:09:24   

Thanks Walaa!!

Obviously I wasn't understanding properly why I got the error!

Once you get your head round it LLBL makes life so much easier!