how can I use ContentManager with multiple order conditions

Topics: Core, Customizing Orchard, General
Feb 25, 2015 at 9:05 AM
In LINQ or EntityFramework syntax, I can use


but in orchard content manager, the only option is


how do i suppose to specify multiple order conditions if ThenBy() is missing?
Feb 25, 2015 at 2:23 PM
The content manager .Query() return an IContentQuery. To limit the database results you can use a .Where() clause, you can also use .OrderBy() for ordering, and indeed there is no .ThenBy() at this point. But, here all is applied on an IContentQuery and, normally, at the end you will use .Slice() or .List() that will return an IEnumerable on which you can use all the Linq expressions as OrderBy() and .ThenBy()

Feb 26, 2015 at 2:28 AM
Thank you jtkech for your reply.

I'm aware that I can convert IContentQuery to IEnumerable in order to perform .ThenBy() syntax. But I think when .Slice() in also involved in the query, I may not get the data list as exactly what I want.

And in that case, I have to get all the data from database, then perform local .OrderBy().ThenBy() which on the other hand, would affect the performance.
Feb 26, 2015 at 5:35 AM
Slice(index, count) is just to skip items and limit the count. You can use .List() (that in fact do a Slice(0, 0), here count = 0 means all) to get all items

That said, you're right, in terms of performance it's better to first work on the IContentQuery to limit the database results, this before the .List() is applied. But before the .List() there is no .ThenBy(). You can try to do two OrderBy() but I think this would have more impact on performance. But, for only the reordering, I think you can do that on the IEnumerable, this after the .List()

Then, you can have something like that
var parts = _contentManager.Query<SomePart, SomePartRecord>("TypeTest")
    .Where(x => x.SomeProperty == SomeThing)
    .OrderBy(x => x.Name)
    .ThenBy(x => x.FirstName)

Mar 6, 2015 at 7:07 AM
thanks, I think that's probably the best thing I can do at the moment.