我正在尝试使用.net客户端查询OData以获取特定DateTime中的Last Published组件。它总是失败,异常'datetime' is not property of entity: 'com.tridion.storage.ComponentMeta'
var lastPubComponents = _client.Components
.Where(p => p.PublicationId == sitePubId && p.LastPublishDate > Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-ddThh:mm:ss")))
.ToList();
当我检查构建的Odata URL时,它看起来像以下一样,我尝试使用浏览器中的相同URL进行双重检查,我得到了相同的错误。
尝试使用LINQ时的Odata网址
/cd_webservice/odata.svc/Components?$filter=PublicationId eq 59 and LastPublishDate ge datetime'2012-10-11T09:22:14'
我尝试从上面的网址中删除datetime
字符串,然后再次从浏览器中进行检查。此URL正常工作,我得到了正确的结果。
以下是网址(工作时没有日期时间)
/cd_webservice/odata.svc/Components?$filter=PublicationId eq 59 and LastPublishDate ge '2012-10-11T09:22:14'
这种情况让我觉得LINQ和/或.net客户端组合出了问题。
我尝试使用不同的日期时间格式组合,但我得到了同样的错误。当我检查Odata标准时,建议使用datetime
字符串,但这是我遇到的问题。
有人知道如何从LINQ代码调试/解决这个问题吗?任何帮助将不胜感激。
答案 0 :(得分:9)
OData规范确实提到了在http://www.odata.org/documentation/overview#AbstractTypeSystem中使用像“LastPublishDate ge datetime'2012-10-11T09:22:14”这样的结构进行查询的可能性,但这并没有在产品中实现。但是,在产品中是OData规范允许的日期方法进行过滤(http://www.odata.org/documentation/uri-conventions#FilterSystemQueryOption)。这些是day(),hour(),minute(),month(),second()和year(),这基本上会转化为一个噩梦般的查询:
DateTime timeNow = DateTime.Now;
int yearNow = timeNow.Year;
int monthNow = timeNow.Month;
int dayNow = timeNow.Day;
int hourNow = timeNow.Hour;
int minuteNow = timeNow.Minute;
int secondNow = timeNow.Second;
var lastPubComponents = service.Components
.Where(p => p.PublicationId == 3 &&
(p.LastPublishDate.Value.Year > yearNow ||
(p.LastPublishDate.Value.Year == yearNow && p.LastPublishDate.Value.Month > monthNow) ||
(p.LastPublishDate.Value.Year == yearNow && p.LastPublishDate.Value.Month == monthNow && p.LastPublishDate.Value.Day > dayNow) ||
(p.LastPublishDate.Value.Year == yearNow && p.LastPublishDate.Value.Month == monthNow && p.LastPublishDate.Value.Day == dayNow && p.LastPublishDate.Value.Hour > hourNow) ||
(p.LastPublishDate.Value.Year == yearNow && p.LastPublishDate.Value.Month == monthNow && p.LastPublishDate.Value.Day == dayNow && p.LastPublishDate.Value.Hour == hourNow && p.LastPublishDate.Value.Minute > minuteNow) ||
(p.LastPublishDate.Value.Year == yearNow && p.LastPublishDate.Value.Month == monthNow && p.LastPublishDate.Value.Day == dayNow && p.LastPublishDate.Value.Hour == hourNow && p.LastPublishDate.Value.Minute == minuteNow && p.LastPublishDate.Value.Second > secondNow)
)
)
.ToList();
希望这有帮助。
此致 丹尼尔。
答案 1 :(得分:0)
试试这个
var lastPubComponents = _client.Components
.Where(p => p.PublicationId == sitePubId && p.LastPublishDate.ToString("yyyyMMdd") < DateTime.Now.ToString("yyyyMMdd")))
.ToList();