Copied to clipboard

Flag this post as spam?

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


  • Onno Sloof 23 posts 43 karma points
    May 28, 2014 @ 08:31
    Onno Sloof
    0

    How to sort on property

    Hi,

    Can somebody help me with the next situation:

    I've a document type (dtProject) with a property 'property1' of type Date Picker.
    I added some test items to the sitestructure and now I want to have a list\collection of these items, ordered by property1.
    I've code below (coll_1 is working, tried coll_2, coll_3, coll_4, but they give 'Error loading MacroEngine script') :

    @inherits umbraco.MacroEngines.DynamicNodeContext

    @{
     var item = Model;
    }

    var config = Model.NodeById(1085);
    var projectsNode = Model;
    if (config.HasValue("config_projectsNode"))
    {
     projectsNode = Model.NodeById(config.config_projectsNode);
    }

    var coll_1 = newsNode.Children.Where("Visible").OrderBy("CreateDate");

    var coll_2 = newsNode.Children.Where("Visible").OrderBy("property1");
    var coll_3 = newsNode.Children.Where("Visible").OrderBy(x => x.GetPropertyValue("property1"));
    var coll_4 = newsNode.Children.Where("Visible").OrderBy(x => x.GetPropertyValue<DateTime>("property1")).ThenBy(x => x.CreateDate);

    Thanks,

    Onno

  • Jeavon Leopold 3074 posts 13632 karma points MVP 11x admin c-trib
    May 28, 2014 @ 10:23
    Jeavon Leopold
    0

    Hi Onno,

    First thing, with Umbraco v7 it is not recommended that you use Scripting Files (dynamic node) this is only supported for legacy. If you are using MVC templates then you can have your Razor logic directly in your View or Partial Views. If you are using MasterPage templates then it's recommend that you use Partial View Macro's.

    So using one of the above, there are two ways to get this collection, either with a typed or dynamic model.

    Typed:

    var myCollection = Model.Content.AncestorOrSelf(1).Descendants("dtProject").OrderBy(x => x.GetPropertyValue<DateTime>("property1"));
    

    Dynamic:

    var myDynCollection = CurrentPage.AncestorOrSelf(1).Descendants("dtProject").OrderBy("property1");
    

    Jeavon

  • Onno Sloof 23 posts 43 karma points
    May 28, 2014 @ 11:48
    Onno Sloof
    0

    Hi Jeavon,

    Tried both Typed and Dynamic in a Partial View.

    Dynamic worked fine, but typed is having problems with nullable of property1

    thanks

  • Jeavon Leopold 3074 posts 13632 karma points MVP 11x admin c-trib
    May 28, 2014 @ 11:52
    Jeavon Leopold
    0

    Great, lets get rid of those null values then:

    var myCollection = Model.Content.AncestorOrSelf(1).Descendants("dtProject").Where(x => x.HasValue("property1")).OrderBy(x => x.GetPropertyValue<DateTime>("property1"));
    
Please Sign in or register to post replies

Write your reply to:

Draft