Sorting Child Collections

Thread is closed for posting
2 posts, 0 answers
  1. Tim
    Tim avatar
    5 posts
    Member since:
    Feb 2013

    Posted 19 Mar 2013 Link to this post

    Is it possible to define the sort order of a Child collection that is returned by a Parent object?
  2. Yordan
    Yordan avatar
    39 posts

    Posted 21 Mar 2013 Link to this post

    Hello Tim,

    It is possible. Sorting of child collection is done in exactly the same manner like sorting an ordinary collection. For example consider the Northwind database. We have a category and we want to sort the child products of that category based on some conditions. This can be done in the following way:
    using (NorthwindContext context = new NorthwindContext())
    var productsByNameAsc = context.Categories.First().Products.OrderBy(p => p.ProductName);
    var productsByNameDesc = context.Categories.First().Products.OrderByDescending(p => p.ProductName);
    var productsBySuppCityAsc = context.Categories.First().Products.OrderBy(p => p.Supplier.City);
    var productsBySuppCityDesc = context.Categories.First().Products.OrderByDescending(p => p.Supplier.City);

    I hope that helps. If this is not your case for sorting child collections please get back to us for assistance.

    the Telerik team
    Free Webinar: OpenAccess Integration in Sitefinity. SIGN UP NOW.
Back to Top