简体   繁体   English

Umbraco 7 LINQ在日期范围内过滤Model子项

[英]Umbraco 7 LINQ to filter Model children on date range

I have a collection of articles in Umbraco 7 which span multiple years and wanted to write a simple LINQ query to get items for the current year: 我在Umbraco 7中收集了一些文章,这些文章跨越了多年,并且想编写一个简单的LINQ查询来获取当年的项目:

var press =  Model.Content.Children.Where(w => Convert.ToDateTime(w.GetPropertyValue("publicationDate")) >= new Date(1,1,2018));

This results in the error: 这导致错误:

Delegate 'System.Func' does not take 1 arguments. 代理'System.Func'不接受1个参数。

I'm contemplating just bunging the data in a list of T - unless someone can steer me in the right direction with filtering on Umbraco content nodes? 我正在考虑仅将数据捆绑在T列表中-除非有人可以通过Umbraco内容节点上的过滤将我引向正确的方向?

Thanks in advance. 提前致谢。

I'm thinking something like this looks more "correct" to me, but it's untested. 我在想类似的东西对我来说看起来更“正确”,但是未经测试。 One thing to note is the way I make sure to get the property as DateTime directly from Umbraco. 需要注意的一件事是,我确保直接从Umbraco将该属性获取为DateTime的方式。 And also I figured comparing it to a DateTime instead of a Date might make a difference. 而且我还想将它与DateTime而不是Date进行比较可能会有所不同。

var press =  Model.Content.Children.Where(w => w.GetPropertyValue<DateTime>("publicationDate") >= new DateTime(2018,1,1));

Your problem is that you're using Date instead of DateTime . 您的问题是您使用的是Date而不是DateTime Date is not a standard .NET type - it is most likely from some other DLL you have referenced in your project. Date不是标准的.NET类型-它很可能来自您在项目中引用的其他DLL。

This means it is not comparable with DateTime which is the type of the data you are converting to when fetching data from Umbraco (and also the type we're using to store dates internally in Umbraco). 这意味着它不能与DateTime相提并论,后者是从Umbraco提取数据时要转换为的数据类型(也是用于内部在Umbraco中存储日期的类型)。

As mentioned in the other answer you should also be using the .GetPropertyValue<DateTime>() method to simply have Umbraco do the conversion for you. 如另一个答案中所述,您还应该使用.GetPropertyValue<DateTime>()方法来简单地让Umbraco为您完成转换。 It would simplify your code a bit. 它将简化您的代码。

Apart from that - your query works fine if you just change it to use DateTime instead of Date - I've just verified that myself. 除此之外-如果您只是将其更改为使用DateTime而不是Date那么您的查询就可以正常工作-我刚刚验证了自己。

Consider putting the date in a variable first to avoid instantiating new instances of DateTime inside the Where : 考虑先将日期放在变量中,以避免在Where内实例化DateTime新实例:

var currentYear = new DateTime(2018, 1, 1);
var press =  Model.Content.Children.Where(x => x.GetPropertyValue<DateTime>("publicationDate") > currentYear);

// or your way:
var press =  Model.Content.Children.Where(w => Convert.ToDateTime(w.GetPropertyValue("publicationDate")) >= date);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM