Copied to clipboard

Flag this post as spam?

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


  • Phil 54 posts 139 karma points
    Jan 07, 2015 @ 16:59
    Phil
    1

    Getting descendants of node using where clause in razor

    Let's say I have a document type with alias BlogPost, which has properties:

    • blogTitle (Text String)
    • blogDate (DateTimePicker)
    • blogBody (Rich Text Editor)

    When getting the latest 5 blogs contained in the site, I would use the following snippet:

    var blogList = CurrentPage.AncestorOrSelf(1).Descendants("BlogPost").OrderBy("blogDate desc").Take(5);
    

    However, I am trying to retrieve the latest 5 blogs where the date lies in a specific range (for example: after 15 December 2014).

    I know that you can use the Where clause with a condition contained in a String, but I am attempting to compare two DateTimes:

    Convert.ToDateTime("blogDate") >= new DateTime("15 Dec 2014")
    

    Is this possible to do with a Where clause?

  • Jeavon Leopold 3074 posts 13632 karma points MVP 11x admin c-trib
    Jan 07, 2015 @ 17:05
    Jeavon Leopold
    102

    Yes, should be like this:

    var blogList = CurrentPage.AncestorOrSelf(1).Descendants("BlogPost").Where("blogDate >= @0", new DateTime("15 Dec 2014")).OrderBy("blogDate desc").Take(5);
    
  • Tony Groome 261 posts 804 karma points
    Jan 07, 2015 @ 17:52
    Tony Groome
    0

    Great - tomorrow's question answered! :)

    Tony

  • Phil 54 posts 139 karma points
    Jan 08, 2015 @ 10:12
    Phil
    0

    That's great Jeavon - thank you.

    I made a mistake in my initial code: new DateTime("15 Dec 2014") should be new DateTime(2014, 12, 15)

    Apart from that, Jeavon's code is perfect.

Please Sign in or register to post replies

Write your reply to:

Draft