Sort by Child Entity

Posts   
 
    
asowles
User
Posts: 46
Joined: 23-Apr-2008
# Posted on: 19-Jun-2009 16:03:41   

I am trying to sort by a child entity. I am using version 2.6. I've set up the code this way:


 EntityCollection<ScheduleTemplateEntity> schedules = new EntityCollection<ScheduleTemplateEntity>(new ScheduleTemplateEntityFactory());
            RelationPredicateBucket bucket = new RelationPredicateBucket();
            bucket.Relations.Add(ScheduleTemplateEntity.Relations.ScheduleDetailEntityUsingCompanyIdScheduleId);
            bucket.Relations.Add(ScheduleDetailEntity.Relations.DaysOfWeekEntityUsingCompanyIdDayOfWeekId);
            SortExpression sort = new SortExpression(DaysOfWeekFields.SortOrder | SortOperator.Ascending);
            adapter.FetchEntityCollection(schedules, null, 0, sort);
            return schedules;

When the query runs, it throws this error:


SD.LLBLGen.Pro.ORMSupportClasses.ORMQueryExecutionException was unhandled



Message="An exception was caught during the execution of a retrieval query: Correlation name 'DaysOfWeek' not found. Check InnerException, QueryExecuted and Parameters of this exception to examine the cause of this exception."



Source="SD.LLBLGen.Pro.ORMSupportClasses.NET20"



RuntimeBuild="08042008"



RuntimeVersion="2.6.0.0"



QueryExecuted="\r\n\tQuery: SELECT [DBA].[ScheduleTemplate].[CompanyID] AS CompanyId, [DBA].[ScheduleTemplate].[ScheduleTemplateID] AS ScheduleTemplateId, 

[DBA].[ScheduleTemplate].[ScheduleTemplateName], [DBA].[ScheduleTemplate].[AdjustToFixedSchedule], [DBA].[ScheduleTemplate].[ArriveEarlyWithinLength], [DBA].[ScheduleTemplate].[ArriveLateWithinLength] FROM [DBA].[ScheduleTemplate] ORDER BY [DBA].[DaysOfWeek].[SortOrder] ASC\r\n"

I am hoping that this is something simple that I have missed.

Thanks for your help!

Allen

asowles
User
Posts: 46
Joined: 23-Apr-2008
# Posted on: 19-Jun-2009 16:48:22   

My apologies. I realized that I had forgotten to include the RelationPredicate bucket in my call to adapter. I have fixed that:


EntityCollection<ScheduleTemplateEntity> schedules = new EntityCollection<ScheduleTemplateEntity>(new ScheduleTemplateEntityFactory());

            IPrefetchPath2 prefetchPath = new PrefetchPath2((int)EntityType.ScheduleTemplateEntity);
            prefetchPath.Add(ScheduleTemplateEntity.PrefetchPathScheduleDetail).SubPath.Add(ScheduleDetailEntity.PrefetchPathDaysOfWeek);

            RelationPredicateBucket bucket = new RelationPredicateBucket();
            bucket.Relations.Add(ScheduleTemplateEntity.Relations.ScheduleDetailEntityUsingCompanyIdScheduleId);
            bucket.Relations.Add(ScheduleDetailEntity.Relations.DaysOfWeekEntityUsingCompanyIdDayOfWeekId);
            
            SortExpression sort = new SortExpression(DaysOfWeekFields.SortOrder | SortOperator.Ascending);
            
            adapter.FetchEntityCollection(schedules, bucket, 0, sort, prefetchPath);
            
            return schedules;

And now the query executes but the collection isn't in the order that I expect (ordered by DayOfWeekFields.SortOrder). How can I get the collection sorted by the desired column?

Thanks,

Allen

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 20-Jun-2009 07:50:36   

As you pass the sort expression variable to the root fetch, the root fetch is ordered by its sub-sub-related entity (DaysOfWeek). You are fetching a graph with three levels. What level do you want to sort?

To sort on a prefetchPath, use the .Add overload that accept a PredicateExpression.

Hope helpful wink

David Elizondo | LLBLGen Support Team
asowles
User
Posts: 46
Joined: 23-Apr-2008
# Posted on: 20-Jun-2009 16:47:31   

Thank you for the response, but you lost me a little. In my example, for each Entity called ScheduleTemplate, there would be 7 ScheduleDetail entities that each have a single DayOfWeek entity. We need to get the ScheduleDetail records in order by the DayOfWeek.SortOrder field.

You say to use a prefetchPath that accepts a PredicateExpression, but don't I need a SortExpression? I thought PredicateExpression was for filtering? Did I misunderstand this?

Can you give me a quick example?

Thanks,

Allen

Walaa avatar
Walaa
Support Team
Posts: 14993
Joined: 21-Aug-2005
# Posted on: 22-Jun-2009 10:34:53   

We need to get the ScheduleDetail records in order by the DayOfWeek.SortOrder field.

As Daelmo said you need to pass the relation and sortExpression to the prefetchPath, as follows.

            EntityCollection<ScheduleTemplateEntity> schedules = new EntityCollection<ScheduleTemplateEntity>(new ScheduleTemplateEntityFactory());

            IRelationCollection relations = new RelationCollection();
            relations.Add(ScheduleDetailEntity.Relations.DaysOfWeekEntityUsingCompanyIdDayOfWeekId);
            
            SortExpression sort = new SortExpression(DaysOfWeekFields.SortOrder | SortOperator.Ascending);

            IPrefetchPath2 prefetchPath = new PrefetchPath2((int)EntityType.ScheduleTemplateEntity);
            prefetchPath.Add(ScheduleTemplateEntity.PrefetchPathScheduleDetail, 0, null, relations, sort);//.SubPath.Add(ScheduleDetailEntity.PrefetchPathDaysOfWeek);
            
            adapter.FetchEntityCollection(schedules, null, 0, null, prefetchPath);          
            return schedules;

Note: Add the SubPath if you want to fetch the DaysOfWeek as well.

asowles
User
Posts: 46
Joined: 23-Apr-2008
# Posted on: 22-Jun-2009 14:49:33   

Thank you very much for the reply. This helps. I've been going through the help and all of the examples and I think that one things I find confusing is that the examples use different syntax wihout really an explanation of why. For example, you used the following code

IRelationCollection relations = new RelationCollection();
            relations.Add(ScheduleDetailEntity.Relations.DaysOfWeekEntityUsingCompanyIdDayOfWeekId);

but in the help, the Relations are created this way:


RelationPredicateBucket bucket = new RelationPredicateBucket();
bucket.Relations.Add(CustomerEntity.Relations.OrderEntityUsingCustomerID);
bucket.Relations.Add(OrderEntity.Relations.OrderDetailsEntityUsingOrderID);
bucket.Relations.Add(OrderDetailsEntity.Relations.ProductEntityUsingProductID);
bucket.Relations.Add(ProductEntity.Relations.SupplierEntityUsingSupplierID);

What is the difference between the two? What reasons would I use one or the other? Are they functionally the same?

Thanks,

Allen

Walaa avatar
Walaa
Support Team
Posts: 14993
Joined: 21-Aug-2005
# Posted on: 22-Jun-2009 15:09:08   

You just need to pay attention to the Reference manual and what parameters are accepted by different methods.

Using the Adapter model, most fetch methods accepts a RelationPredicateBucket, which is just a container or a bucket (hence the name) which holds a PredicateExpression and a RelationCollection objects.

These 2 are separated in the overloads of the PrefetchPath.Add() method. That's why I created a RelationCollection and passed it to the Add() method, instead of creating a RelationPredicate bucket.

I think V.S. intellisense mighthelp you figure out which paramter is needed for each method. (and for each overload).