使用 Razor LINQ .Where() 查找具有特定日期值的 umbraco 节点 [英] Using Razor LINQ .Where() to find umbraco nodes with a certain date value

查看:37
本文介绍了使用 Razor LINQ .Where() 查找具有特定日期值的 umbraco 节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在重写 XSLT 宏以显示当前页面的子节点,具体取决于为月"和年"设置的查询字符串变量.这用于显示特定时期文章的新闻列表页面.

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.

在旧的宏中,我循环并选择newsDate"属性(这是一个日期选择器字段)的月份部分所在的节点,并将它们分配给 nodelist 变量.$Displaymonth 从查询字符串中收集.

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>

我在使用 razor 语法创建类似的节点列表时遇到问题.假设查询字符串月份是八月,我尝试过类似

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))

调试错误主要是抱怨我的 newsDate 变量中没有属性month".或者'Func`2'类型中不存在任何属性或字段日期".无论我做什么,它似乎都将我的 Datepicker 属性视为字符串,如 此处 但我使用的是最新版本的 umbraco.

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.

如何通过转换 datepicker 属性(Umbraco 中的 DateTime 对象)的月/年并将其与变量进行比较来找到子级?我什至如何在 .Where 语句中获取此日期属性并提取月/年?

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?

推荐答案

注意:这种形式的 LINQ 语法是 umbraco DynamicNode 对象所特有的(大约来自 umbraco v4.7.1)

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

如果您使用 values 字典,其中字典中的项目属于 DateTime 类型,并且您要比较的属性是日期属性,那么会执行某种类型的强制转换,您将获得所需的功能,这就是我的方法已经做到了:

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);

这篇关于使用 Razor LINQ .Where() 查找具有特定日期值的 umbraco 节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆