3
votes

I'm currently rewriting an XSLT macro to display child nodes of the current page, depending on what querystring variables are set for 'month' and 'year'. This is used for a news listings page which displays articles for a certain period.

In the old macro, I am looping through and selecting nodes where the month part of the "newsDate" property (which is a datepicker field) and assigning them to the nodelist variable. $Displaymonth is gathered from querystring.

<xsl:for-each select="$currentPage/*[@isDoc]">
          <xsl:sort order="descending" select="newsDate" data-type="text"/>
          <xsl:if test="umbraco.library:FormatDateTime(newsDate, '%M') = $displayMonth">
            <xsl:copy-of select="." />
          </xsl:if>
</xsl:for-each>

I am having trouble creating a similar list of nodes using razor syntax. Assuming the querystring month is August, I have tried things like

Model.Children.Where(umbraco.library.FormatDateTime(newsDate,'M') + " == 8");
Model.Children.Where("Convert.ToDateTime(newsDate).Month == \"8\"");
Model.Children.Where("newsDate.Month == \"8\"");
Model.Children.Where("newsDate.Value.Month == \"8\"");
Model.Children.Where(i=>Convert.ToDateTime(i.GetProperty("newsDate").Value).Month==8))

The debug errors mostly complain that there is no property "month" inside my newsDate variable. Either that or "No property or field date exists in type 'Func`2'". It seems to be treating my Datepicker property as a string whatever I do, as described here but I am using the latest version of umbraco.

How can I find children by converting the month/year of a datepicker property (a DateTime object within Umbraco) and comparing that to a variable? How can I even get this date property and extract the month/year, while inside a .Where statement?

2
Where accepts a lambda expression so you're using a wrong syntax. Look it up.Dan Abramov
Also, there is no DatePicker data type in C# so I have no idea what newDate is and can't give you an example.Dan Abramov
umbraco.com/follow-us/blog-archive/2011/3/1/… - "With DynamicObject (DynamicNode too, because it inherits from), the c# compiler / razor parser doesn't allow you to use the familiar lambda syntax to filter your sets. This is because we now return a DynamicObject [DynamicNodeList] to allow method chaining."giles
Datepicker is just a DateTime object with an embedded calendar control within the CMSgiles

2 Answers

8
votes

Note: This form of LINQ syntax is peculiar to umbraco DynamicNode objects (from about umbraco v4.7.1)

If you use the values dictionary where the item in the dictionary is of type DateTime, and the property you're comparing against is a date property then some type coercion is performed and you will get the functionality you need, here's how I've done it:

var values = new Dictionary<string,object>();
values.Add("queryStartDate", DateTime.Parse(Request["queryStartDate"])) ;
values.Add("queryEndDate", DateTime.Parse(Request["queryEndDate"])) ;
var results = Model.Children.Where("newsDate > queryStartDate && newsDate < queryEndDate", values);
4
votes

If you want to use a Linq Where clause you can do this: CurrentModel.ChildrenAsList.Where(...) (so not Model but CurrentModel...it's easier in visual studio too, gives you intellisense ;) )