locked
Nested OrderBy Question RRS feed

  • Question

  • I have an object, "Category", that contains a collection of "Subcategories".

    I retrieve these objects thus:

    Context.Set<Category>().Where(c => c.OwnerID == OwnerID).Include(c => c.Subcategories).OrderBy(c => c.Name)

    I get back an ordered set of Categories, but each subcollection of Subcategories is unordered.  I am looking for the simplest, most elegant way of modifying this statement to simultaneously sort the subcategories by their respective names.

    Any ideas?

    Thanks

     


    Bob Cohen
    Monday, January 2, 2012 11:06 PM

All replies

  • Hi Bob,

    Welcome!

    I think can try in this way:

    Context.Set<Category>().Where(c => c.OwnerID == OwnerID)
    .Select(t=>new{CId=t.CId,SubCategories=t.SubCategories.Ordery(s=>s.Name)})
    .OrderBy(c => c.Name)
    

    Have a nice day. 


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, January 3, 2012 7:14 AM
  • If I understand what you've got here, it's a projection onto a new, anonymous type that contains the category ID and the sorted subcategory list.

    If this is true, then it's not quite what I'm looking for.  I'd want the result to be a complete Category object with the sorted sublist, not just the ID.  I would have to put every category field into the projection and maintain each occurrance of this logic.

    Is this correct?

    Thanks


    Bob Cohen
    Tuesday, January 3, 2012 3:37 PM
  • Hi Bob,

    Thanks for your feedback.

    You can put all field in the Anonymous Type:

    Context.Set<Category>().Where(c => c.OwnerID == OwnerID)
    .Select(t=>new{CId=t.CId, Name=t.Name, Others=t.Others,SubCategories=t.SubCategories.Ordery(s=>s.Name)})
    .OrderBy(c => c.Name)
    
    

    Have a nice day.


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, January 4, 2012 6:34 AM
  • Yes, I understand I can keep stringing out values in the anonymous type, but that defeats my request for an "elegant" solution.  Each time that I change the Category class, I would have to update each and every occurrence of the Select statement.  I'm really looking for something where I wouldn't have to do that.

     

     


    Bob Cohen
    Wednesday, January 4, 2012 6:37 AM
  • Hi Bob,

    Thanks for your feedback.

    It seems there is not a good way to achieve your goal, thanks for understanding.

    Have a nice day.


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, January 5, 2012 9:03 AM
  • Anyone else?
    Bob Cohen
    Thursday, January 5, 2012 2:59 PM