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.
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.
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.
'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?)
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.
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:
Jeroen
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.
Hello,
Could you show me the code?
So instead of this:
You should do this:
Don't use var because than it's still dynamic.
Jeroen
Here's my code:
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?)
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:
Jeroen
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?
Bump... still stuck here.
Anyone?
is working on a reply...