Ordering a query issue

Posted by Community Admin on 04-Aug-2018 11:56

Ordering a query issue

All Replies

Posted by Community Admin on 20-Feb-2013 00:00

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?

Posted by Community Admin on 20-Feb-2013 00:00

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

Posted by Community Admin on 20-Feb-2013 00:00

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.

This thread is closed