Copied to clipboard

Flag this post as spam?

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


  • Craig Willard 21 posts 126 karma points
    Feb 11, 2015 @ 05:39
    Craig Willard
    0

    Razor Macro, sorting by numeric field

    I've inherited an Umbraco 4.7 project and am trying to sort out why a Razor query is not sorting properly and how to fix it.

    I have a Document Type with a Property of type Label called articleViews, which simply keeps track of the # of times an article was viewed.

    We want to run a query to show the "most viewed" articles.

    var articles= @Model.NodeById(1203) .Descendants() .Where("NodeTypeAlias==\"..."") .OrderBy("articleViews desc");

    The query is "working", but is sorting the articleViews field as a string rather than a number, so, the Top 3 articleViews totals are 99, 981, and 979. There are several items with >1000 views, but those are not being sorted properly.

    I tried using OrderByDescending(x => x.articleViews) but am getting a .NET 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"

    I could use some help here. I feel like the solution is probably not too complicated.

  • Jeroen Breuer 4861 posts 12138 karma points MVP 3x admin c-trib
    Feb 11, 2015 @ 09:10
    Jeroen Breuer
    0

    Hello,

    Umbraco 4.7 Razor is all dynamic and you can't use OrderByDescending(x => x.articleViews) because extension methods don't work on dynamic objects. There is a walkthourgh here which might help: http://umbraco.com/follow-us/blog-archive/2011/2/23/umbraco-47-razor-feature-walkthrough-%E2%80%93-part-1

    It's probably best to just not use dynamics here. So debug te code and check what @Model.NodeById(1203) .Descendants() .Where("NodeTypeAlias==\"..."") returns. Probably something like DynamicNodeList.

    Than something like this should work:

    DynamicNodeList articles = @Model.NodeById(1203).Descendants().Where("NodeTypeAlias==\"..."");
    articles.OrderByDescending(x => Convert.ToInt32(x.articleViews))

    Jeroen

  • Craig Willard 21 posts 126 karma points
    Feb 11, 2015 @ 16:13
    Craig Willard
    0

    Thanks for the advice, Jeroen. I am still running into issues unfortunately.

    I can confirm that @articles is returning an object of type umbraco.MacroEngines.DynamicNodeList

    When I attempt to do articles.OrderByDescending(x => Convert.ToInt32(x.articleViews)) I get the same 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."

    I've gotten this far before but I'm not sure of the correct way to cast/convert the type.

  • Jeroen Breuer 4861 posts 12138 karma points MVP 3x admin c-trib
    Feb 11, 2015 @ 16:16
    Jeroen Breuer
    0

    Hello,

    Could you show me the code?

    So instead of this:

    var articles= @Model.NodeById(1203).Descendants().Where("NodeTypeAlias==\"..."");

    You should do this:

    DynamicNodeList articles = @Model.NodeById(1203).Descendants().Where("NodeTypeAlias==\"..."");

    Don't use var because than it's still dynamic.

    Jeroen

  • Craig Willard 21 posts 126 karma points
    Feb 11, 2015 @ 16:27
    Craig Willard
    0

    Here's my code:

    @using umbraco.MacroEngines;
    @inherits umbraco.MacroEngines.DynamicNodeContext
    @{
        var contentRoot = @Model.NodeById(1203); //home node
    
        DynamicNodeList articles= contentRoot
                    .Descendants()
                    .Where("NodeTypeAlias!=\"ArticlesContainer\"");
    }
    <h3>Popular Articles</h3>
    <ul>@foreach (var article in articles.OrderByDescending(x => Convert.ToInt32(x.articleViews))) { ... }</ul>

    And I'm now getting this error:

    'umbraco.MacroEngines.DynamicNodeList' does not contain a definition for 'OrderByDescending' and no extension method 'OrderByDescending' accepting a first argument of type 'umbraco.MacroEngines.DynamicNodeList' could be found (are you missing a using directive or an assembly reference?)

  • Jeroen Breuer 4861 posts 12138 karma points MVP 3x admin c-trib
    Feb 11, 2015 @ 16:30
    Jeroen Breuer
    0

    DynamicNodeList inherits from IEnumerable<DynamicNode> so OrderByDescending should work. Did you include the System.Linq namespace?

    So add this at the top of your Razor file:

    @using System.Linq;

    Jeroen

  • Craig Willard 21 posts 126 karma points
    Feb 11, 2015 @ 16:33
    Craig Willard
    0

    Jeroen,

    I had not included the reference @usingSystem.Linq; but I just added it and am receiving the same error as in my previous post.

    Any ideas on what the issue might be?

  • Craig Willard 21 posts 126 karma points
    Feb 19, 2015 @ 06:15
    Craig Willard
    0

    Bump... still stuck here.

    Anyone?

  • 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