Copied to clipboard

Flag this post as spam?

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


  • Tom Cowling 144 posts 342 karma points
    Feb 01, 2016 @ 11:56
    Tom Cowling
    0

    OrderBy For loop

    Hey guys,

    I'm having a bit of trouble trying to order some results from a for loop. I'm not very good with razor, so getting the syntax right is killing my brain off! I'm not sure if I need to be looking into the .Where expression or the for loop itself.

    I'd like the months to be outputted by publishDate Descending.

    @helper RenderMonthChildren(umbraco.MacroEngines.DynamicNode month)
    {
        var posts = month.Children.Where(c => c.Visible && c.NodeTypeAlias == "BlogEntry");
        if(posts.Any())
        {
            foreach(var post in posts)
            {
                <div class="blog-post-summary">
                    <h3><a href="@post.Url">@post.GetPropertyValue("heading")</a></h3>
                    <p class="blog-date"><em>@DateTime.Parse(post.GetPropertyValue("publishDate")).ToString("ddd, dd MMMM yyyy")</em></p>
                    <p>@post.GetPropertyValue("summary")<br /><br /></p>
                    <p class="blog-read-more"><a href="@post.Url">Read more</a></p>
                </div>
            }
        }
    }
    

    Any help would be much appreciated!

    Thanks,

    Tom

  • Dennis Aaen 4499 posts 18254 karma points admin hq c-trib
    Feb 01, 2016 @ 12:22
    Dennis Aaen
    0

    Hi Tom,

    Could you then try to do something like this to see if this will work for you.

    @helper RenderMonthChildren(umbraco.MacroEngines.DynamicNode month)
    {
        var posts = month.Children.Where(c => c.Visible && c.NodeTypeAlias == "BlogEntry");
        if(posts.Any())
        {
            foreach(var post in posts.OrderByDescending(x => x.publishDate)
            {
                <div class="blog-post-summary">
                    <h3><a href="@post.Url">@post.GetPropertyValue("heading")</a></h3>
                    <p class="blog-date"><em>@DateTime.Parse(post.GetPropertyValue("publishDate")).ToString("ddd, dd MMMM yyyy")</em></p>
                    <p>@post.GetPropertyValue("summary")<br /><br /></p>
                    <p class="blog-read-more"><a href="@post.Url">Read more</a></p>
                </div>
            }
        }
    }
    

    Hope this helps,

    /Dennis

  • Tom Cowling 144 posts 342 karma points
    Feb 01, 2016 @ 12:27
    Tom Cowling
    0

    Thanks for the quick response Dennis.

    Sorry - forgot to mention that publishDate was a field I have added. Would this explain the following error?

    error CS1061: 'umbraco.MacroEngines.DynamicNode' does not contain a definition for 'publishDate' and no extension method 'publishDate' accepting a first argument of type 'umbraco.MacroEngines.DynamicNode' could be found (are you missing a using directive or an assembly reference?)
    
  • Dennis Aaen 4499 posts 18254 karma points admin hq c-trib
    Feb 01, 2016 @ 12:30
    Dennis Aaen
    0

    Hi Tom,

    Okay what if you just do:

    foreach(var post in posts.OrderBy("publishDate, desc")
    

    Would this work for you then, or are you getting the same error?

    /Dennis

  • Tom Cowling 144 posts 342 karma points
    Feb 01, 2016 @ 12:32
    Tom Cowling
    0

    Slightly different error this time..

    error CS0411: The type arguments for method 'System.Linq.Enumerable.OrderBy(System.Collections.Generic.IEnumerable, System.Func)' cannot be inferred from the usage. Try specifying the type arguments explicitly.
    
  • Lars-Erik Aabech 349 posts 1100 karma points MVP 7x c-trib
    Feb 01, 2016 @ 12:46
    Lars-Erik Aabech
    101

    Try posts.OrderByDescending(x => x.GetPropertyValue("publishDate")). :)

  • Tom Cowling 144 posts 342 karma points
    Feb 01, 2016 @ 12:48
    Tom Cowling
    1

    Perfect. :)

    Thanks Lars-Erik.

Please Sign in or register to post replies

Write your reply to:

Draft