Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Claushingebjerg 939 posts 2574 karma points
    Nov 12, 2015 @ 09:45
    Claushingebjerg
    0

    How to sort items?

    Im trying to sort the items rendered by nested content like:

     @{
    var items = CurrentPage.GetPropertyValue<IEnumerable<IPublishedContent>>("afh").OrderBy("startdato");
        foreach(var item in items) {
    
        @Umbraco.Field(item, "startdato")
        }
    

    }

    But i get the errror

    'System.Collections.Generic.List<Umbraco.Core.Models.IPublishedContent>' does not contain a definition for 'OrderBy'
    

    How do i correctly sort the items?

  • Lee Kelleher 4026 posts 15837 karma points MVP 13x admin c-trib
    Nov 12, 2015 @ 09:51
    Lee Kelleher
    0

    Hi Claus,

    I'm not familiar with the string syntax in .OrderBy("startdato"), I guess this is an Umbraco extension method? (I typically use lambda syntax)

    I guess this might be similar to the issue you had previously, namespace references are missing?

    Try adding...

    @using Umbraco.Core
    @using Umbraco.Web
    

    ...to the top of your Razor partial?

    Hope that helps?

    Cheers,
    - Lee

  • Claushingebjerg 939 posts 2574 karma points
    Nov 12, 2015 @ 10:06
    Claushingebjerg
    0

    That didn't do the trick, but maybe you could show a snippet on how to.

    "startdato" is a date picker. I want to sort by the date picker and then take 1. So im looking for the right way to do:

    var items = CurrentPage.GetPropertyValue<IEnumerable<IPublishedContent>>("afh").OrderBy(x => x.startdato).Take(1);
    
  • Lee Kelleher 4026 posts 15837 karma points MVP 13x admin c-trib
    Nov 12, 2015 @ 10:16
    Lee Kelleher
    1

    You could try...

    .OrderBy(x => x.GetPropertyValue<DateTime>("startdato"))
    

    Cheers,
    - Lee

  • Claushingebjerg 939 posts 2574 karma points
    Nov 12, 2015 @ 10:21
    Claushingebjerg
    0
    var items = CurrentPage.GetPropertyValue<IEnumerable<IPublishedContent>>("afh").OrderBy(x => x.GetPropertyValue<DateTime>("startdato"));
    

    Gives me the error

    Compiler Error Message: CS1977: Cannot use a lambda expression as an argument to a dynamically dispatched operation without first casting it to a delegate or expression tree type
    

    I wonder if ill ever learn razor :|

  • Lee Kelleher 4026 posts 15837 karma points MVP 13x admin c-trib
    Nov 12, 2015 @ 10:29
    Lee Kelleher
    101

    Ah, I think it's the CurrentPage being dynamic. Try swapping it out with Model.Content, see if that works?

  • Claushingebjerg 939 posts 2574 karma points
    Nov 12, 2015 @ 10:35
    Claushingebjerg
    1

    Bingo!

    You're on the 2016 "i owe you beer at codegarden" list.

    That's the one thing i hate most about razor. The possibillity of doing things more than one way, and the two ways being 99% similar, but then not really... The hours i've spent...

  • Lee Kelleher 4026 posts 15837 karma points MVP 13x admin c-trib
    Nov 12, 2015 @ 11:16
    Lee Kelleher
    0

    You're welcome! :-)

    With Razor, when I switched over from using XSLT, I got confused with the dynamic/typed approaches. I ended up ignoring all the dynamic stuff and stuck with typed-objects.

    Mixing both dynamic & typed approaches caused me a world of pain ;-)

    Cheers,
    - Lee

  • Osman Coskun 170 posts 404 karma points
    Apr 22, 2016 @ 20:34
    Osman Coskun
    0

    Thanks for the tips.

    What wolud be the syntax if we needed to change sort order by date?

    Edit:

    OrderBy should be switched by OrderByDescending to change the sort direction.

    Thank you ;)

  • Osman Coskun 170 posts 404 karma points
    Aug 07, 2016 @ 12:02
    Osman Coskun
    0

    I would like to sort the nested content from another page, i use the following code and it gives error ( CS1977: Cannot use a lambda expression as an argument to a dynamically dispatched operation without first casting it to a delegate or expression tree type).

    var timelineNode= Umbraco.Content(CurrentPage.AncestorOrSelf(1).Descendants("timeline").First().Id);
    var datesList = timelineNode.GetPropertyValue<IEnumerable<IPublishedContent>>("dates").OrderByDescending(x => x.GetPropertyValue<DateTime>("date"));
    

    timeline is doc type, dates is nested content data type property.

    If i test without ordering it works.

    var timelineNode= Umbraco.Content(CurrentPage.AncestorOrSelf(1).Descendants("timeline").First().Id);
    var datesList = timelineNode.GetPropertyValue<IEnumerable<IPublishedContent>>("dates");
    

    Any ideas?

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies