Copied to clipboard

Flag this post as spam?

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


  • Robin Hansen 135 posts 368 karma points
    Aug 05, 2014 @ 09:43
    Robin Hansen
    0

    Lambda sort on custom property

    I've a custom property on my doctypes ("publishdate") which is a DatePicker.

    In my foreach loop I would like to check if this property has a value, and if so - check the value comparet to the current Date...

    This is what I got so far, with no luck yet :|:

    var startNode = Umbraco.TypedContent(Model.MacroParameters["startNodeID"].ToString());

    foreach(startNode.DescendantsOrSelf(4).Where(x => x.GetProperty("publishdate") != null ? x.GetPropertyValue("publishdate") >= DateTime.Now() : ""))

     

  • Dan Lister 416 posts 1974 karma points c-trib
    Aug 05, 2014 @ 10:14
    Dan Lister
    1

    Hi Robin,

    Instead of using the GetProperty method and GetPropertyValue method, you could try using the GetPropertyValue<> method instead where by you pass the type of the property value. So for example:

    var startNode = Umbraco.TypedContent(Model.MacroParameters["startNodeID"].ToString());
    foreach (startNode.DescendantsOrSelf(4).Where(x => x.GetPropertyValue<DateTime>("publishdate") >= DateTime.Now()))
    {
        // ....
    }
    
  • Robin Hansen 135 posts 368 karma points
    Aug 05, 2014 @ 10:20
    Robin Hansen
    0

    I've tried that allready - however, not all "publishdate" has a value so I need to do a check in my expression, as showed in my first example :|

  • Dan Lister 416 posts 1974 karma points c-trib
    Aug 05, 2014 @ 10:29
    Dan Lister
    100

    Just noticed a couple of things in your original code example. The foreach loop is incorrect. The below should work.

    @{
        var startNode = Umbraco.TypedContent(123);
        foreach (var node in startNode.DescendantOrSelfs(4).Where(x => 
            x.HasProperty("publishdate") && 
            x.GetPropertyValue<DateTime>("publishdate") >= DateTime.Now))
        {
            // ...
        }
    }
    

    Thanks, Dan.

  • Dan Lister 416 posts 1974 karma points c-trib
    Aug 05, 2014 @ 10:39
    Dan Lister
    0

    Just a thought, you might want to use the UpdateDate property as all IPublishedContent instances will have a value for this.

    @{
        var startNode = Umbraco.TypedContent(123);
        foreach (var node in startNode.DescendantOrSelfs(4).Where(x => x.UpdateDate >= DateTime.Now))
        {
            // ...
        }
    }
    

    Thanks, Dan.

  • Robin Hansen 135 posts 368 karma points
    Aug 05, 2014 @ 10:51
    Robin Hansen
    0

    Yes! - it was this snippet I was looking for:

    x.HasProperty("publishdate")

    It now works as intenteed... - however, I don't need the additional .Children in my foreach loop :) 

  • Dan Lister 416 posts 1974 karma points c-trib
    Aug 05, 2014 @ 10:52
    Dan Lister
    0

    Sorry Robin. I've edited my posts accordingly. Its amazing how difficult it is to differentiate between DescendantOrSelfs() and DescendantOrSelf().

    :)

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies