locked
Sort Order RRS feed

  • Question

  • User-284642143 posted

    I have some products belonging to a category, sub category (3 tables in total). An example:

    Samsung (Category)
    TV (Sub Category) >> TVModelAndSize (product)
    Fridge Freezer (Sub Category) >> FridgeFreezerModel (product)
    Blu Ray Player (Sub Category) >> BlurayPlayerModel (product)
    DVD Player (Sub Category) >> DVDPlayerModel (product)

    As you can tell, i can have plenty of models for various blu ray players, TVs etc. Each table has a SortOrder column.

    What i would like to do is order by Category then Sub Category and then Product.

    My code to achieve this is 

    var Products = from pr in myDc.Products.OrderBy(c=> c.Category.SortOrder).OrderBy(sc=> sc.SubCategory.SortOrder).OrderBy(p=> .Product.SortOrder) where pr.InStock;

    When the products are displayed they seem to be in the order of product sort order (so Category and Sub Category are not in order). If i remove the product order code so the code is 

    var Products = from pr in myDc.Products.OrderBy(c=> c.Category.SortOrder).OrderBy(sc=> sc.SubCategory.SortOrder) where pr.InStock;

    Then the Cat and Sub Cat are in order but products arent?

    How could i get it to do what i require?

    Saturday, March 25, 2017 2:14 PM

Answers

  • User-832373396 posted

    Hi EssCee,

    I suggest that you could use  .ThenBy( ) after  first orderBy(), just like code below:

    var Products = from pr in myDc.Products.OrderBy(c=> c.Category.SortOrder)
    .ThenBy(sc=> sc.SubCategory.SortOrder).ThenBy(p=> .Product.SortOrder) where pr.InStock;

    Best regards,

    Jolie

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, March 26, 2017 2:38 PM