Copied to clipboard

Flag this post as spam?

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


  • David Podmore 15 posts 73 karma points
    May 19, 2015 @ 15:28
    David Podmore
    0

    OrderBy() using lambda?

    Hi,

    Im using a lambda expression to sort a collection of child pages (news articles).

    @{
        Layout = "MASTER.cshtml";
        var a = CurrentPage.Children().Where("Visible");
        List<dynamic> b = new List<dynamic>();
        foreach(dynamic item in a) {
            b.Add(item);
        }
        var articles = b.OrderByDescending(x => (x.dateOverride.ToString("yyyy") != "0001" ? x.dateOverride : x.CreateDate ));
    }
    

    Initially I tried using a.OrderBy(expression) but cannot do so, as a is dynamic (I get the error "Cannot use a lambda expression as an argument to a dynamically dispatched operation without first casting it to a delegate or expression tree type").

    Is my above code a correct/efficient way to achieve what I'm after?

    Thanks.

  • Mehul Gajjar 48 posts 172 karma points
    May 21, 2015 @ 15:20
    Mehul Gajjar
    0

    Hi David,

    you can also try with IPublishedContent for taking children like below

    IPublishedContent a = CurrentPage.Children().Where("Visible");

    you can alos see below URL for Umbraco Helper

    https://github.com/umbraco/Umbraco4Docs/blob/master/Documentation/Reference/Querying/UmbracoHelper/index.md

    UmbracoHelper is very useful for such kind of operations.

    Hope it will help you.

    Regards,

    Mehul Gajjar.

Please Sign in or register to post replies

Write your reply to:

Draft