More in this section

Forums / Developing with Sitefinity / Ordering a query issue

Ordering a query issue

3 posts, 0 answered
  1. Nick
    Nick avatar
    113 posts
    Registered:
    21 Oct 2008
    20 Feb 2013
    Link to this post

    Can anyone point me in the right direction here please. I'm querying products by department and trying to order them by Title:

    products = catManager.GetProducts().Where(p => p.GetValue<TrackedList<Guid>>("Department").Contains(Department.Id) && p.IsActive).OrderBy(p => p.Title);

    This doesn't order them by title, it keeps them in the same order they were created in. 

    Can anyone suggest what the issue is here?

  2. Daniel Plomp
    Daniel Plomp avatar
    952 posts
    Registered:
    18 Feb 2004
    20 Feb 2013 in reply to Nick
    Link to this post

    Hi Nick,

    That is pretty strange. For me, the following code works:

    // Define the managers
    var manager = TaxonomyManager.GetManager();
    var catManager = CatalogManager.GetManager();
                 
    // Get the Id of the department
    var departmentName = "Books";
    var departmentId = manager.GetTaxa<HierarchicalTaxon>().Where(t => t.Name == departmentName).SingleOrDefault().Id;
     
    // Get all products, who are assigned to this department
    var products = catManager.GetProducts().Where(p => p.GetValue<TrackedList<Guid>>("Department").Contains(departmentId) && p.IsActive).OrderBy(x => x.Title);

    Just be sure to add the OrderBy at the end of any operation, because not all LINQ methods preserve Order.

    Kind regards,
    Daniel

  3. Nick
    Nick avatar
    113 posts
    Registered:
    21 Oct 2008
    20 Feb 2013 in reply to Daniel Plomp
    Link to this post

    Thanks for confirming Daniel. I was sure that was right. 

    Some further testing, and I think this comes down to a localisation issue. Despite all the products in the locale I'm querying having Titles, I suspect some of them don't have Titles in the original locale which has since been deleted. I've experienced a number of issues with locales during an import and I think this is another issue that's been left over. I'm pretty sure this is the case because I've deleted the products in that department and reimported (now the new locale is the default) and the code is ordering correctly now.

    So in the end, problem solved. Still some confusion over the initial issue though.

3 posts, 0 answered