Copied to clipboard

Flag this post as spam?

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


  • Sebastiaan Janssen 5045 posts 15476 karma points MVP admin hq
    Feb 23, 2011 @ 12:29
    Sebastiaan Janssen
    0

    Sorting a list of items?

    I was under the impression that sorting was built in, but when I try something like this:

                <umbraco:macro runat="server" language="razor">
                  @{
                    var allPages = Model.umbTextpage;
                    var ordered = allPages.OrderByDescending(x => x.Id);
                  }
                  @foreach(var t in ordered){
                    <p>@t.Id</p>
                  }
                </umbraco:macro>

    All I get is a nice 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

    Should I be using uQuery / UQL if I need to sort?

  • Gareth Evans 142 posts 334 karma points c-trib
    Feb 23, 2011 @ 12:52
    Gareth Evans
    0

    Nope, it's built in, just not how you expect it to work... :)

    Use Model.umbTextPage.OrderBy("Id")

    If you want multicolumn, descending:

    Model.umbTextPage.OrderBy("Id, Name descending")

    I'm posting some articles to the umbraco blog over the next few days introducing the new features with examples

  • Jonas Eriksson 930 posts 1825 karma points
    Feb 23, 2011 @ 13:03
    Jonas Eriksson
    0

    Cool, but OrderBy("Id descending") throws me an error. OrderBy("Id") works fine.

  • Gareth Evans 142 posts 334 karma points c-trib
    Feb 23, 2011 @ 13:05
    Gareth Evans
    0

    Maybe it's desc?

    What's the error?

  • Jonas Eriksson 930 posts 1825 karma points
    Feb 23, 2011 @ 13:12
    Jonas Eriksson
    0

    "Error loading Razor Script sitemap.cshtmlSyntax error"

    @{
      var sortedList = Model.Parent.Children.OrderBy("Id descending"); 
      foreach(var n in sortedList)
      {
       <p>@n.Name</p>  
       }
      }

    Same with "Id desc" I'm afraid.

     

  • Gareth Evans 142 posts 334 karma points c-trib
    Feb 23, 2011 @ 13:14
    Gareth Evans
    0

    Okay, must be an issue with starting with a decending sort, I will test and fix

  • Sebastiaan Janssen 5045 posts 15476 karma points MVP admin hq
    Feb 23, 2011 @ 13:19
    Sebastiaan Janssen
    0

    Great, that works (except for descending). Would be best to have it as:

    Model.umbTextPage.OrderByDescending("Id")
  • Gareth Evans 142 posts 334 karma points c-trib
    Feb 23, 2011 @ 13:23
    Gareth Evans
    0

    The way the parser works is that OrderBy is converted to an expressiontree, with nested calls to OrderBy/OrderByDescending/ThenBy/ThenByDescending internally

    Could add a shorthand OrderByDescending that can only handle one column though, it's probably not required though.

    I don't think I can make it work directly like LINQ: e.g: Model.umbTextPage.OrderByDescending("Id").ThenBy("Name") , although i'll investigate it

  • Sebastiaan Janssen 5045 posts 15476 karma points MVP admin hq
    Feb 23, 2011 @ 14:00
    Sebastiaan Janssen
    0

    It already works when I use the latest code from Codeplex by the way! 

    Nah, no direct need to add the shorthand then, we just need some good documentation, I know this stuff is hard. :-)

  • Gareth Evans 142 posts 334 karma points c-trib
    Feb 23, 2011 @ 14:02
    Gareth Evans
    0

    OrderBy("id desc") works with codeplex code but not the 4.7 beta?

    I haven't checked the bug/tested the code yet- seems odd

  • Jonas Eriksson 930 posts 1825 karma points
    Feb 23, 2011 @ 14:48
    Jonas Eriksson
    0

    Hm, it is the beta. Umbraco v 4.7.0.beta (Assembly version: 1.0.4071.3793)

  • Sebastiaan Janssen 5045 posts 15476 karma points MVP admin hq
    Feb 23, 2011 @ 15:03
    Sebastiaan Janssen
    0

    Aha, apologies, it didn't actually work completely, it works when I sort multiple colums:

    OrderBy("PublishDateOverrule, CreateDate descending")

  • Jonas Eriksson 930 posts 1825 karma points
    Mar 16, 2011 @ 14:14
    Jonas Eriksson
    0

    Hm, shouldnt this be working?

    foreach (dynamic n in Model.Children.OrderBy("CreateDate desc"))
    {
    <p>@n.Name</p>
    }

    it gives me the same result as

    foreach (dynamic n in Model.Children.OrderBy("CreateDate"))
    {
    <p>@n.Name</p>
    }

    4.7 release

  • Sebastiaan Janssen 5045 posts 15476 karma points MVP admin hq
    Mar 16, 2011 @ 14:28
    Sebastiaan Janssen
    0

    Same problem here, the list is in descending order by default, and multicolumn searches now give errors (4.7 final).

  • Darren Ferguson 1022 posts 3259 karma points MVP c-trib
    Mar 24, 2011 @ 10:22
    Darren Ferguson
    0

    Guys: Has this been filed as an issue on codeplex or shall i do it?

  • Jonas Eriksson 930 posts 1825 karma points
    Mar 24, 2011 @ 11:32
    Jonas Eriksson
    0

    yes please (I should have, but...)

  • Stephan Lonntorp 195 posts 212 karma points
    Mar 26, 2011 @ 22:39
  • Gareth Evans 142 posts 334 karma points c-trib
    Mar 26, 2011 @ 22:54
    Gareth Evans
    0

    Thanks Stephan,

    I've fixed this in the main trunk, but your workitem says OrderBy("NodeName desc")

    Can you confirm if this was meant to be accessing a custom property or the Name property?

    They're implemented differently.

    Gareth

  • Stephan Lonntorp 195 posts 212 karma points
    Mar 26, 2011 @ 23:30
    Stephan Lonntorp
    0

    I have tried:

    .OrderBy("CreateDate desc") and .OrderBy("CreateDate")

    .OrderBy("UpdateDate desc") and .OrderBy("UpdateDate")

    .OrderBy("Name desc") and .OrderBy("Name")

    .OrderBy("Date desc") and .OrderBy("Date") (a property with an alias of "date")

    .OrderBy("Heading desc") and .OrderBy("Heading") (a property with an alias of "heading")

    The actual value, (but I have stepped through code to look at the value, and it's passed as stated above), are passed in with a PreValue property, DropDown, where I have specifeid the values as described above (camel cased).

    Nothing does anything to the resulting list.

     

    This is part of my code: (paste unformatted on a mac is fu**ing impossible)

    @helper GetListingFromContent(dynamic listRoot, string orderBy, bool sortByDesc, int itemsPerPage, bool showExpanded)     {              if (listRoot == null || listRoot.GetType().Equals(typeof(DynamicNull)))         {             return;         }          var orderByStatement = String.Empty;          if (!String.IsNullOrWhiteSpace(orderBy))         {             orderByStatement = String.Concat(orderBy, (sortByDesc ? " desc" : String.Empty));         }          var listItems = listRoot.Children             .Where("HasAccess")             .Where("!IsProtected")             .Where("template > 0");          if (!String.IsNullOrWhiteSpace(orderByStatement))         {             listItems = listItems.OrderBy(orderByStatement);         }          var totalItemCount = listItems.Count();          if (totalItemCount <= 0)         {             return;         }          if (itemsPerPage.Equals(0))         {             itemsPerPage = totalItemCount;         }
Please Sign in or register to post replies

Write your reply to:

Draft