Sort by field in related table with LLBLGenProDataSource2

Posts   
 
    
XOR
User
Posts: 5
Joined: 15-Jan-2008
# Posted on: 15-Jan-2008 03:40:06   

Hi

I am using version 2.5 and I am trying to get my LLBLGenProDataSource2 datasource to sort by a field from a related table, included in the prefetch path.

Code in front


 <llblgenpro:llblgenprodatasource2 id="_CommentsGeneralDS" runat="server" adaptertypename="Model.DatabaseSpecific.DataAccessAdapter, ModelDBSpecific"
        datacontainertype="EntityCollection" entityfactorytypename="Model.FactoryClasses.GenCommentEntityFactory, Model" >
        </llblgenpro:llblgenprodatasource2>

Code behind


protected void Page_Load(object sender, EventArgs e)
    {
        IPrefetchPath2 prefetchPath = new PrefetchPath2((int)EntityType.GenCommentEntity);
        prefetchPath.Add(GenCommentEntity.PrefetchPathPriorityCategory);
        _CommentsGeneralDS.PrefetchPathToUse = prefetchPath;

        SortExpression sort = new SortExpression();
        //sort.Add(PriorityCategoryFields.OrderPosition | SortOperator.Descending);
        sort.Add(GenCommentFields.CreationDate | SortOperator.Descending);
        _CommentsGeneralDS.SorterToUse = sort;
        _CommentsGeneralDS.Refetch = true;

    }

But if I uncomment the addition of the first sort parameter the sql statement fails because the PriorityCategory table is not included in the sql FROM clause. I have been searching online for the last 2 hours for the correct way to do this but have found nothing. I would like to avoid sorting the gridview's items manually if I can and any help is most appreciated.

thanks in advance for any tips XOR

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 15-Jan-2008 08:41:25   

To sort on a related field (field from another table), you should provide the relation (Join) to that other table.

XOR
User
Posts: 5
Joined: 15-Jan-2008
# Posted on: 15-Jan-2008 08:43:43   

Thanks Walaa

But how do you do that on an llblgenprodatasource2 object?

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 15-Jan-2008 08:48:56   

Add the relation to the _CommentsGeneralDS.FilterToUse.Relations collection.

XOR
User
Posts: 5
Joined: 15-Jan-2008
# Posted on: 15-Jan-2008 08:56:37   

Cheers mate!

XOR
User
Posts: 5
Joined: 15-Jan-2008
# Posted on: 24-Jan-2008 03:38:51   

Hi

I finally got time back on this project to perform this sorting and I use the following code


protected void Page_Load(object sender, EventArgs e)
    {
        IPrefetchPath2 prefetchPath = new PrefetchPath2((int)EntityType.GenCommentEntity);
        prefetchPath.Add(GenCommentEntity.PrefetchPathPriorityCategory);
        _CommentsGeneralDS.PrefetchPathToUse = prefetchPath;

        _CommentsGeneralDS.FilterToUse = new RelationPredicateBucket();
     _CommentsGeneralDS.FilterToUse.Relations.Add(GenCommentEntity.Relations.PriorityCategoryEntityUsingPriorityCategoryId);

        SortExpression sort = new SortExpression();
        sort.Add(PriorityCategoryFields.OrderPosition | SortOperator.Descending);
        sort.Add(GenCommentFields.CreationDate | SortOperator.Descending);
        _CommentsGeneralDS.SorterToUse = sort;
        _CommentsGeneralDS.Refetch = true;

    }


and I get the error: The multi-part identifier "dbo.priority_category.order_position" could not be bound.

And this is because the sql still only includes the single GenComment table in the from clause (as seen in SQL profiler). Did I miss a step to get this working?

Cheers in advance for your help!

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 24-Jan-2008 07:38:36   

Seems that this should work. Could you please:

  • Provide us the resulted generated SQL (See LLBLGen Pro Help - Using the generated code - Troubleshooting and debugging ).

  • Provide us the declarative ASPX of the LLBLGenProDataSource object.

  • Check what value have .FilterToUse at the last line of your code snippet

  • Why you don't have an "if (!IsPostback) {...}"... maybe this is not related simple_smile

David Elizondo | LLBLGen Support Team
XOR
User
Posts: 5
Joined: 15-Jan-2008
# Posted on: 24-Jan-2008 08:49:23   

thanks daelmo

I actually discovered that I was clearing the filterToUse in another pre data bind event handler.

thanks for your assistance!