3

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(2014, 12, 15)

Is this possible to do with a Where clause?

LoveFortyDown
  • 1,011
  • 2
  • 17
  • 37

1 Answers1

6

The snippet to do this is as follows:

var blogList = CurrentPage.AncestorOrSelf(1).Descendants("BlogPost").
               .Where("blogDate >= @0", new DateTime(2014, 12, 15))
               OrderBy("blogDate desc")
               .Take(5)

This snippet was taken from a reply on the following Umbraco forum topic.

LoveFortyDown
  • 1,011
  • 2
  • 17
  • 37