How to order the items in a nested LINQ-provided collection

Posted by Carson McComas on Stack Overflow See other posts from Stack Overflow or by Carson McComas
Published on 2010-05-21T01:33:10Z Indexed on 2010/05/21 1:40 UTC
Read the original article Hit count: 269

Filed under:
|
|

I've got a (SQL Server) database table called Category. And another database table called SubCategory.

SubCategory has a foreign key relationship to Category. Because of this, thanks to LINQ, each Cateogory has a property called SubCategories and LINQ is nice enough to return all the SubCategories associated with my Category when I grab it.

If I want to sort the Categories alphabetically, I can just do:

return db.Categories.OrderBy(c => c.Name);

However, I have no idea how to order the SubCategories collection inside each Category.

My goal is to return a collection of Categories, where all of the SubCategory collections inside of them are ordered alphabetically by Name.

© Stack Overflow or respective owner

Related posts about LINQ

Related posts about linq-to-sql