0

During an update of our website with Umbraco, we upgraded from 4.7.2 to 4.11.8.

In a CSHTML-File I had this code:

foreach(var item in Model.AncestorOrSelf("Master")
                         .Items.First()
                         .PublicationFolder.First()
                         .Children.Where("Visible")
                         .OrderBy("publicationType, date desc"))

It worked fine and sorted the collection first by publicationType and then by the newest date.

In the new version (4.11.8) it doesn't work anymore. It gives me an exception: At least one object must implement IComparable.

And if I write .OrderBy("publicationType", "date desc"), it doesn't affect the collection.

So is this a bug or am I doing anything wrong? Is there a workaround?

1
  • Yes I found a solution. I will write an answer for that.
    – bvwidt
    Commented Aug 8, 2013 at 10:23

3 Answers 3

5

I found a solution so I need to cast the collection to a List<DynamicNode> so that it works.

foreach (var item in ((List<DynamicNode>)@Model.AncestorOrSelf("Master")
    .Items.First()
    .PublicationFolder.First()
    .Children.Where("Visible").Items)
    .OrderBy(t => t.GetPropertyValue("publicationType"))
    .ThenByDescending(t => t.GetPropertyValue("date")))
1
  • Brian, actually that's almost exactly the same as mine answer. We are casting the same things, the only difference being that you are doing an implicit cast, and I am casting using the Cast<T>() method.
    – Digbyswift
    Commented Aug 8, 2013 at 10:46
1

The Where("Visible") extension returns IQueryable<DynamicNode> so you should be able to apply the LINQ OrderBy() and OrderByDescending() syntax for this query. You may need to alter certain bits though:

foreach(var item in Model.AncestorOrSelf("Master")
                     .Items.First()
                     .PublicationFolder.First()
                     .Children.Where("Visible")
                     .Cast<DynamicNode> // This line is optional
                     .OrderBy(x => x.GetPropertyValue("publicationType")
                     .ThenByDescending(x => x.GetPropertyValue("date"))

I have included an optional line to show that you can explicitly cast the dynamic objects to DynamicNode objects and you can then use intellisense in your view.

0

If memory serves you should be able to chain the OrderBys

foreach(var item in Model.AncestorOrSelf("Master").Items.First().PublicationFolder.First().Children.Where("Visible").OrderBy("publicationType").OrderByDescending("date"))
2
  • It could be the solution, but I'm getting this error: 'umbraco.MacroEngines.DynamicNodeList' does not contain a definition for 'OrderByDescending'. Is there anything missing?
    – bvwidt
    Commented Aug 7, 2013 at 14:43
  • Chaining the ordering seems to override it with each part of the chain, so this has a limited use-case and doesn't solve the original question. For me the other solutions (using .ThenByDescending) below worked better as I needed to order by several different properties concurrently, which is the crux of the question too. Good to know you can chain like this though, so thanks for this answer :)
    – Dan
    Commented Feb 28, 2014 at 9:46

Not the answer you're looking for? Browse other questions tagged or ask your own question.