Sorting by PrefetchPath field which is the same entity type as the base query.

Posts   
 
    
techpro
User
Posts: 8
Joined: 08-Dec-2021
# Posted on: 08-Dec-2021 21:08:35   

I have a TaskCategory table and it has a ParentCategoryId field which links to itself so you can have a parent and child task category.

I have a list that combines the parent and child so you can select them from a combo box, i.e.:

Parent Category Name 1 --> Child Category Name 1 Parent Category Name 1 --> ChildCategory Name 2 Parent Category Name 2 --> Child Category Name 3

I would like to sort the list by the Parent Category Name and then the Child Category name but I can't quite figure out how.

The code below is only ordering by the child category name...but I want it to sort by the parent category name first which is the related entity in the prefetch path.

I'd appreciate any help that you can provide...

  var qf = new QueryFactory();
  var q = qf.TaskCategory

  .Where(TaskCategoryFields.IsDeleted.Equal(false).Or(TaskCategoryFields.IsDeleted.IsNull()))
  .AndWhere(TaskCategoryFields.IsVerified.Equal(true))
  .AndWhere(TaskCategoryFields.ParentCategoryId.NotEqual(0))

  .WithPath(TaskCategoryEntity.PrefetchPathParentCategory
    .WithOrdering(TaskCategoryFields.Name.Ascending())
  )
        
  .OrderBy(TaskCategoryFields.Name.Ascending());
daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 09-Dec-2021 07:16:41   

Hi techpro,

Since you are already using prefetchpath, you could fetch it the other way: first the parent categories and prefetch the child categories. Somnething like:

var qf = new QueryFactory();
  var q = qf.TaskCategory

  .Where(TaskCategoryFields.IsDeleted.Equal(false).Or(TaskCategoryFields.IsDeleted.IsNull()))
  .AndWhere(TaskCategoryFields.IsVerified.Equal(true))
  .AndWhere(TaskCategoryFields.ParentCategoryId.Equal(0))

  .WithPath(TaskCategoryEntity.PrefetchPathChildCategories
    .WithOrdering(TaskCategoryFields.Name.Ascending())
  )
        
  .OrderBy(TaskCategoryFields.Name.Ascending());

Does that work for you?

David Elizondo | LLBLGen Support Team
techpro
User
Posts: 8
Joined: 08-Dec-2021
# Posted on: 09-Dec-2021 20:48:44   

Yes, I was able to make it work as you suggested so thank you!

I was curious if the way I was doing it was possible. If I was prefetching a different entity type, it seems like my code would have worked, but I thought there might be some way since they are the same entity types to add like an alias or something identifier so I could select the prefretchpath entity's name as the sort.

But thank you.