ODATA DATE QUERY [英] ODATA DATE QUERY

查看:119
本文介绍了ODATA DATE QUERY的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要返回日期为4/4/2013的记录(例如)。
ODATA服务中的日期字段返回为{DATE:2013-04-04T17:39:33.663}

I need to return records that have a date of 4/4/2013 (for example). The date field in the ODATA service returns as {DATE: "2013-04-04T17:39:33.663"}

查询应如何查看喜欢?

到目前为止,我已经看到三个选择,但没有一个是直接的。

So far, I have seen three options to do this, but none of them are straight forward.

选项一:datetime'2013-04-04T12:00':这需要时间的规定

Option one: datetime'2013-04-04T12:00': this requires specification of time

选项二:月(DATE)eq 04 day(DATE)eq 04年(DATE)eq 2013:这很麻烦

Option two: month(DATE) eq 04 day(DATE) eq 04 year(DATE) eq 2013: this is cumbersome

选项三:使用'ge'和'le'的选项一个在两个日期之间获取记录:这也是crap。

Option three: using option one with 'ge' and 'le' to get records between 2 dates: this is also crap.

这应该非常简单,因为它在T-SQL中。

This should be very simple as it is in T-SQL.

推荐答案

OData v3没有原始数据类型,只是 Date 。您拥有的属性是 DateTime DateTimeOffset ,所以无论您是否使用它是该值的时间部分,如果要检查值是否相等,则必须检查时间组件。

OData v3 doesn't have a primitive data type that's just Date. The property you have is either a DateTime or a DateTimeOffset, so, whether you're using it or not, there is a time portion of that the value, and if you want to check the value for equality, the time component must be checked as well.

如果您确定您不会使用时间部分(并始终设置为00:00),您可以执行以下查询:

If you know for sure that you never use the time portion (and always set it to 00:00), you could do the following query:

/service.svc/EntitySet?$filter=DateProperty+eq+datetime'2013-04-04'

这意味着00:00的时间部分。但这只是速记。如果某些DateTimes的时间部分不是00:00,那么可能会有意想不到的结果。

which implies a time portion of 00:00. But that's just shorthand. And you could have unexpected results if some of your DateTimes do wind up with time portions that are not 00:00.

所以,考虑到你只是想查看日期价值的一部分,而不是完全平等,我认为你提到的第二种方法是最好的方法:

So, given that you just want to check the date portion of the value, and not full equality, I think the second approach you mention is the best way of going about it:

/service.svc/EntitySet?$filter=day(DateProperty)+eq+4+and+month(DateProperty)+eq+4+and+year(DateProperty)+eq+2013

这样你正在检查你的意思是要检查,没有更多的。

That way you're checking exactly what you mean to be checking and nothing more.

为什么值得,我相信一个 Date 数据类型正在OData v4中。那么你可以使用平等检查,而不用担心时间。

For what it's worth, I believe a Date datatype is coming in OData v4. Then you'll be able to use equality checking without worrying about the time.

这篇关于ODATA DATE QUERY的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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