Copied to clipboard

Flag this post as spam?

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


  • Mike Chambers 636 posts 1253 karma points c-trib
    Oct 11, 2012 @ 01:20
    Mike Chambers
    0

    How to filter empty datetime picker in razor script?

    I have an expiredate as a documentType parameter...

    so I want to filter items that have expiredate < now... and also where the expiredate isn't set...

    I can't seem to find a way to do that..

    var items = node.Children.Where("expiredate > DateTime.Now || expiredate == False")

    was my first guess... but that doesn't work...

    although

    var items = node.Children.Where("expiredate == False") does get all the ones with no dates set..

    but var items = node.Children.Where("expiredate == False || expiredate == True") doesn't get all the items????

    Any ideas?

  • Mike Chambers 636 posts 1253 karma points c-trib
    Oct 11, 2012 @ 01:22
    Mike Chambers
    0

    forgot to add umb4.8 - (4.9/10 no good for me as content editor broken in medium trust)

  • Mike Chambers 636 posts 1253 karma points c-trib
    Oct 11, 2012 @ 19:23
    Mike Chambers
    0

    Been banging my head against this on today....

    thought I'd be able to leverage HasValue or isNull.... but nope..

     

    DynamicNodeList items = node.Children;
    if (items.Items.Count() > 0)
        { 
            @foreach (DynamicNode item in items)
            {
    (@item.IsNull("ExpireDate")) @item.Name
                    @traverse(item)
            }
        }

     This lists out all the children and gives correct true/false for when the expiredate has no value (DynamicNull)

    However, using the where to try and filter for only those that have are null... gives no items :-( (same for the converse HasValue)

    DynamicNodeList items = node.Children.Where("ExpireDate.isNull()");
    if (items.Items.Count() > 0)
        { 
            @foreach (DynamicNode item in items)
            {
    (@item.IsNull("ExpireDate")) @item.Name
                    @traverse(item)
            }
        }

     It's driving me insance that I can't do something this simple!

  • SC Digital Services Team 104 posts 171 karma points
    Oct 12, 2012 @ 17:19
    SC Digital Services Team
    0

    Not sure if this would work in Razor, but I've got something similar in an event handler I wrote - try using the DateTime.TryParse() on your ExpireDate parameter, with a proper DateTime variable as the outputted value (either from the conversion or from giving it a value on error using DateTime.MinValue). 

    The new variable should be in the right format to compare to DateTime.Now for your 'where' or 'if' conditions.

     

  • Mike Chambers 636 posts 1253 karma points c-trib
    Oct 12, 2012 @ 22:03
    Mike Chambers
    0

    Thanks for the pointer, but this was about trying to filter on an empty date... I can't trap an error and give it a minValue either... as it doesn't give me an error...

    reading up the current implementation of razor automatically correctly types dateTimes, so 

    expiredate > DateTime.Now, works as expected ;-)

     

    Still trying to find a work around for the HasValue and IsNull in the where("") syntax returns unexpected results... as opposed to @item.param.HasValue and IsNull which work as expected.

  • Mike Chambers 636 posts 1253 karma points c-trib
    Oct 12, 2012 @ 23:31
    Mike Chambers
    0

    In the end had to revert back to lambda expressions... and parsing strings into dates.... but clunky...

     

    var nodes = new DynamicNode(node.Id).GetChildrenAsList.Items
                            .Where(x => DateTime.Parse(x.HasValue("fidusCMSExpireDate", x.GetPropertyValue("fidusCMSExpireDate"), DateTime.Now.AddHours(1).ToString()).ToString()) > DateTime.Now);

     

    Crux of it seems to be that whilst 

     

    DynamicNodeList items = node.Children.Where("ExpireDate.HasValue()");

    even if ExpireDate on that node is empty HasValue() in the Where returns true... but then in the susequent iteration @item.HasValue("ExpireDate") tells me that some of the values contained in the node list actaully HasValue = False!

Please Sign in or register to post replies

Write your reply to:

Draft