EntityCollection and Sorting on Fields on Related Fields

Posts   
 
    
lotek
User
Posts: 56
Joined: 14-Sep-2005
# Posted on: 28-Mar-2008 02:46:18   

Is it possible to sort an EntityCollection on fields on related fields?

Does that work with the datasource control?


EntityCollection<CsReportsEntity> c = new EntityCollection<CsReportsyEntity>(new CsReportsEntityFactory());

IPrefetchPath2 prefetchPath = new PrefetchPath2((int)EntityType.CsWorklistsHistoryEntity);
prefetchPath.Add(CsReportsEntity.PrefetchPathRunHistory);

ISortExpression sorter = new SortExpression(RunHistoryFields.HistoryDate | SortOperator.Ascending);

adapter.FetchEntityCollection(c, null, prefetchPath);

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 28-Mar-2008 10:32:13   

To sort a collection on a related entity's field, you should add a relation to the related entity. Same as when you want to Order By on a field in another table you should join to that table.

I have some comments on your code: 1- You don't need the factory in the first line, it can be used as:

EntityCollection<CsReportsEntity> c = new EntityCollection<CsReportsyEntity>();

2- Since you are fetching CsReportsEntity collection the prefetchPath should be:

IPrefetchPath2 prefetchPath = new PrefetchPath2((int)EntityType.CsReportsyEntity);
prefetchPath.Add(CsReportsEntity.PrefetchPathRunHistory);

3- If you are using prefetchPath just to sort the main CsReportsEntity collection, then you don't need the prefetchPath, you just need to pass the relation between CsReportsEntity & the RunHistoryEntity.

4- If you want to prefetch the RunHistory collection and sorted, then pass the sortExpression to the prefetchPath.Add() method.