The problem: I Have a line-up of artist on a site. I want to give an artist a certain weight to express its importants. Now i give an artist a weight between 1 to 10. When i use OrderBy("weight desc") or OrderByDescending I get this list:
with list.OrderBy(x => int.Parse(x.Weight)) I get this 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 at System.Web.Compilation.AssemblyBuilder.Compile() at System.Web.Compilation.BuildProvidersCompiler.PerformBuild() at System.Web.Compilation.BuildManager.CompileWebFile(VirtualPath virtualPath) at System.Web.Compilation.BuildManager.GetVPathBuildResultInternal(VirtualPath virtualPath, Boolean noBuild, Boolean allowCrossApp, Boolean allowBuildInPrecompile, Boolean throwIfNotFound, Boolean ensureIsUpToDate) at System.Web.Compilation.BuildManager.GetVPathBuildResultWithNoAssert(HttpContext context, VirtualPath virtualPath, Boolean noBuild, Boolean allowCrossApp, Boolean allowBuildInPrecompile, Boolean throwIfNotFound, Boolean ensureIsUpToDate) at System.Web.Compilation.BuildManager.GetVirtualPathObjectFactory(VirtualPath virtualPath, HttpContext context, Boolean allowCrossApp, Boolean throwIfNotFound) at System.Web.Compilation.BuildManager.CreateInstanceFromVirtualPath(VirtualPath virtualPath, Type requiredBaseType, HttpContext context, Boolean allowCrossApp) at System.Web.WebPages.VirtualPathFactoryManager.CreateInstance[T](String virtualPath) at System.Web.WebPages.WebPageBase.CreateInstanceFromVirtualPath(String virtualPath, VirtualPathFactoryManager virtualPathFactoryManager) at umbraco.MacroEngines.RazorMacroEngine.ExecuteRazor(MacroModel macro, INode currentPage) at umbraco.MacroEngines.RazorMacroEngine.Execute(MacroModel macro, INode currentPage)
Have you already considered the "GetChildrenAsList" method on the parent node? Then you should get an actual IList or something similar on which you can apply the check that Alex mentioned.
Maye you can try to just add the prefix "0" to the weight property in yout original list when the weight is < 10 (or weight/length == 1). Then you should be able to just sort on the property lile you did initially.
So, something like this:
@inherits umbraco.MacroEngines.DynamicNodeContext @{ var allNodes =@Model.AncestorOrSelf().Descendants("Artist"); foreach(var node in allNodes) if (node.Weight.Length < 2) node.Weight = "0" + node.Weight; var nodesToDisplay = allNodes.OrderBy("Weight desc").Take(10); var firstID = nodesToDisplay.First().imageId; <ul class="cloudtag">@foreach(var artist in nodesToDisplay){ <li class="tag@(artist.weight)"><a href="@artist.Url" rel="@urls">@artist.Name</a></li> }</ul> }
“Error 237 Cannot use a lambda expression as an argument to a dynamically dispatched operation without first casting it to a delegate or expression tree type ”
The solution came in a form of “uComponents.Core.uQueryExtensions”. the uComponents package is a must, i really approve those guise so many useful datatypes.. Well back to the subject uComponents is a topic of its own, that i promise i’ll blog about in the future.
@using uComponents.Core
@using uComponents.Core.uQueryExtensions
@inherits DynamicNodeContext
@{
var homePage = uQuery.GetCurrentNode().GetAncestorOrSelfNodes().FirstOrDefault(node => node.Level() == 1);
var subItems = homePage.GetChildNodes().Where(node => node.GetPropertyAsBoolean("naviHide"));
@foreach (var item in subItems)
{
//do your thing
}
}
so @model gets you the current node but i lack my Linq, and i did start writing extension methods before i found about the “uComponents.Core.uQueryExtensions” methods anyway. So until i learn otherwise i will use uQueryExtensions and linq for my filtering.
Razor .orderBy integer problem.
Umbraco version: Umbraco v 4.7.2.
The problem: I Have a line-up of artist on a site. I want to give an artist a certain weight to express its importants. Now i give an artist a weight between 1 to 10. When i use OrderBy("weight desc") or OrderByDescending I get this list:
Artist1 weight of: 1
Artist3 weight of: 10
Artist2 weight of: 9
As you can see, it looks like the list is being sorted as text.
What i want is this:
Artist3 weight of: 10
Artist2 weight of: 9
Artist1 weight of: 1
Now is my datatype (dropdown list) saved as an integer, but cannot get it sorted the way I want.
How can i get a correctly sorted list?
Thanks in advance!
You should cast your text value into integer. Something like that:
list.OrderBy(x -> int.Parse(x.Weight))
with list.OrderBy(x => int.Parse(x.Weight)) I get this 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 at System.Web.Compilation.AssemblyBuilder.Compile()
at System.Web.Compilation.BuildProvidersCompiler.PerformBuild()
at System.Web.Compilation.BuildManager.CompileWebFile(VirtualPath virtualPath)
at System.Web.Compilation.BuildManager.GetVPathBuildResultInternal(VirtualPath virtualPath, Boolean noBuild, Boolean allowCrossApp, Boolean allowBuildInPrecompile, Boolean throwIfNotFound, Boolean ensureIsUpToDate)
at System.Web.Compilation.BuildManager.GetVPathBuildResultWithNoAssert(HttpContext context, VirtualPath virtualPath, Boolean noBuild, Boolean allowCrossApp, Boolean allowBuildInPrecompile, Boolean throwIfNotFound, Boolean ensureIsUpToDate)
at System.Web.Compilation.BuildManager.GetVirtualPathObjectFactory(VirtualPath virtualPath, HttpContext context, Boolean allowCrossApp, Boolean throwIfNotFound)
at System.Web.Compilation.BuildManager.CreateInstanceFromVirtualPath(VirtualPath virtualPath, Type requiredBaseType, HttpContext context, Boolean allowCrossApp)
at System.Web.WebPages.VirtualPathFactoryManager.CreateInstance[T](String virtualPath)
at System.Web.WebPages.WebPageBase.CreateInstanceFromVirtualPath(String virtualPath, VirtualPathFactoryManager virtualPathFactoryManager)
at umbraco.MacroEngines.RazorMacroEngine.ExecuteRazor(MacroModel macro, INode currentPage)
at umbraco.MacroEngines.RazorMacroEngine.Execute(MacroModel macro, INode currentPage)
maybe something like this:
int.Parse(node.getProperty("myProperty").Value)
could you take me more code ))
The code:
int.Parse(node.getProperty("myProperty").Value)
doesn't work ?
Nope.
I try:
Allnodes.ToList().OrderBy<Expression<Func<Node, int>>>(x => int.Parse(x)).Take(15);
it always cast to dynamic!
Hi Michael,
Have you already considered the "GetChildrenAsList" method on the parent node? Then you should get an actual IList or something similar on which you can apply the check that Alex mentioned.
Hope this helps.
Cheers,
Michael.
Yes, but no results.
AND!!! I create a Crapy and working script!
@functions {
class Artist
{
public int Id { get; set; }
public int Weight { get; set; }
public string ImageId { get; set; }
public string Name { get; set; }
public string Url { get; set; }
}
}
@{
var artists = new List();
var artistNodes = @Model.AncestorOrSelf().Descendants("Artist");
foreach (var a in artistNodes)
{
artists.Add(new Artist { Id = a.Id, Weight = (int)Convert.ToInt32(a.Weight ), ImageId = a. ImageId , Name = a.Name, Url = a.Url });
}
var Allnodes = artistNodes.OrderBy(x => x.Weight).Take(15);
}
So, I can use the artists vars array :)
But still be crappy solution.
orderBy on integers Doesn't work in umbraco, is this a bug?
Maye you can try to just add the prefix "0" to the weight property in yout original list when the weight is < 10 (or weight/length == 1). Then you should be able to just sort on the property lile you did initially.
So, something like this:
Still crappy though :-)
Cheers,
Michael.
Tnx. So I can also use the values in the datatype dropdown. and replace
1 to 01
2 to 02
3 to 03 ect..
I found some solution :
“Error 237 Cannot use a lambda expression as an argument to a dynamically dispatched operation without first casting it to a delegate or expression tree type ”
The solution came in a form of “uComponents.Core.uQueryExtensions”. the uComponents package is a must, i really approve those guise so many useful datatypes.. Well back to the subject uComponents is a topic of its own, that i promise i’ll blog about in the future.
so @model gets you the current node but i lack my Linq, and i did start writing extension methods before i found about the “uComponents.Core.uQueryExtensions” methods anyway. So until i learn otherwise i will use uQueryExtensions and linq for my filtering.
http://maanehunden.wordpress.com/2012/05/20/to-use-model-or-not-in-umbraco-razor-makroes/
@michael
Yes, if you replace your dropdown values, then it should be even easier and you should be back to your original code :-)
Cheers,
Michael.
tnx Michael! good solutions!
Great :-) !
is working on a reply...