Copied to clipboard

Flag this post as spam?

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


  • FarmFreshCode 225 posts 422 karma points
    Jun 22, 2011 @ 14:33
    FarmFreshCode
    0

    OrderBy a Specific Value - Members in Razor

    Hello Everyone!
    I am currently using v4.7.0 and I have a page that lists all of my "members" wich is actually a list of teachers. I want to display them on this page in order of their last name. I was hoping that the code below would allow me to do this however I get an error.

        var members = Member.GetAll;  
        foreach(var item in members.OrderBy("facultyLastName desc")){
          var facultyFname = item.getProperty("facultyFirstName").Value.ToString();
          var facultyLname = item.getProperty("facultyLastName").Value.ToString();
    ...

    The error I get is...

    Error loading Razor Script 
    .......\Umbraco\App_Data\TEMP\Razor\inline-8c470d31e3635414de480b6d98382976.cshtml(245): error CS0411: The type arguments for method 'System.Linq.Enumerable.OrderBy(System.Collections.Generic.IEnumerable, System.Func)' cannot be inferred from the usage.
    Try specifying the type arguments explicitly.

    I'm not sure if my code is wrong or if I fall into the problem listed here:
    http://our.umbraco.org/forum/developers/razor/19400-OrderBy-not-working?p=0

    If I do fall into that I guess I'll have to wait till v4.7.1 until I can get it fixed :-(

  • Sebastiaan Janssen 5060 posts 15522 karma points MVP admin hq
    Jun 22, 2011 @ 14:43
    Sebastiaan Janssen
    1

    I think the members collection can be ordered like this:

     

    foreach (var item in members.OrderByDescending(x => x.getProperty("facultyLastName").Value.ToString())) {
    //do stuff
    }
  • FarmFreshCode 225 posts 422 karma points
    Jun 22, 2011 @ 15:48
    FarmFreshCode
    0

    Ok that worked.. Thanks Sebastiaan Janssen, but I guess I need it Ascending so that last names starting with "A" come first. If I change OrderByDecending to OrderByAscending I get an error.

    error CS1061: 'System.Array' does not contain a definition for 'OrderByAscending' and no extension method 'OrderByAscending' accepting a first argument of type 'System.Array' could be found (are you missing a using directive or an assembly reference?)

    Is sorting it that way done differently?

     

  • FarmFreshCode 225 posts 422 karma points
    Jun 22, 2011 @ 15:50
    FarmFreshCode
    0

    Nevermind... duh... just "OrderBy"... thanks!

Please Sign in or register to post replies

Write your reply to:

Draft