Copied to clipboard

Flag this post as spam?

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


  • michael Netonline 18 posts 62 karma points
    Jun 05, 2012 @ 08:46
    michael Netonline
    0

    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!

  • Alex Skrypnyk 6132 posts 23951 karma points MVP 7x admin c-trib
    Jun 05, 2012 @ 10:02
    Alex Skrypnyk
    0

    You should cast your text value into integer. Something like that:

    list.OrderBy(x -> int.Parse(x.Weight)) 

  • michael Netonline 18 posts 62 karma points
    Jun 05, 2012 @ 10:29
    michael Netonline
    0

     

    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)

  • Alex Skrypnyk 6132 posts 23951 karma points MVP 7x admin c-trib
    Jun 05, 2012 @ 10:51
    Alex Skrypnyk
    0

    maybe something like this:

    int.Parse(node.getProperty("myProperty").Value)

    could you take me more code  ))

  • michael Netonline 18 posts 62 karma points
    Jun 05, 2012 @ 11:04
    michael Netonline
    0

    The code:

     

    @inherits umbraco.MacroEngines.DynamicNodeContext
    @{
      var allNodes = @Model.AncestorOrSelf().Descendants("Artist").OrderBy(x => int.Parse(x.Weight)).Take(10);  
      var firstID =  allNodes.First().imageId;
    <ul class="cloudtag">@foreach(var artist in allNodes){ 
    <li class="tag@(artist.weight)"><a href="@artist.Url" rel="@urls">@artist.Name</a></li> 
    }</ul>
    }
    
    
    
  • Alex Skrypnyk 6132 posts 23951 karma points MVP 7x admin c-trib
    Jun 05, 2012 @ 11:10
    Alex Skrypnyk
    0

    int.Parse(node.getProperty("myProperty").Value)

    doesn't work ?

  • michael Netonline 18 posts 62 karma points
    Jun 05, 2012 @ 11:52
    michael Netonline
    0

    Nope.

    I try:

    Allnodes.ToList().OrderBy<Expression<Func<Node, int>>>(x => int.Parse(x)).Take(15);

    it always cast to dynamic!

  • Michael Latouche 504 posts 819 karma points MVP 3x c-trib
    Jun 05, 2012 @ 12:18
    Michael Latouche
    0

    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.

  • michael Netonline 18 posts 62 karma points
    Jun 05, 2012 @ 12:35
    michael Netonline
    0

    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?

     

  • Michael Latouche 504 posts 819 karma points MVP 3x c-trib
    Jun 05, 2012 @ 12:53
    Michael Latouche
    0

    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>
    }

    Still crappy though :-)

    Cheers,

    Michael.

  • michael Netonline 18 posts 62 karma points
    Jun 05, 2012 @ 13:10
    michael Netonline
    0

    Tnx. So I can also  use  the values in the datatype dropdown.  and replace

    1 to 01

    2 to 02

    3 to 03 ect..

     

  • Alex Skrypnyk 6132 posts 23951 karma points MVP 7x admin c-trib
    Jun 05, 2012 @ 13:25
    Alex Skrypnyk
    0

    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.

    @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.

     

    http://maanehunden.wordpress.com/2012/05/20/to-use-model-or-not-in-umbraco-razor-makroes/

  • Michael Latouche 504 posts 819 karma points MVP 3x c-trib
    Jun 05, 2012 @ 13:31
    Michael Latouche
    0

    @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.

  • michael Netonline 18 posts 62 karma points
    Jun 05, 2012 @ 13:49
    michael Netonline
    0

    tnx Michael! good solutions!

  • Michael Latouche 504 posts 819 karma points MVP 3x c-trib
    Jun 05, 2012 @ 14:18
    Michael Latouche
    0

    Great :-) !

Please Sign in or register to post replies

Write your reply to:

Draft