问题描述
有没有办法比较 Linq2Sql
中的两个 DateTime
变量但忽略时间部分.
Is there a way to compare two DateTime
variables in Linq2Sql
but to disregard the Time part.
应用程序将项目存储在数据库中并添加发布日期.我想保留确切的时间,但仍然能够按日期本身提取.
The app stores items in the DB and adds a published date. I want to keep the exact time but still be able to pull by the date itself.
我想比较 12/3/89 12:43:34
和 12/3/89 11:22:12
并让它忽略实际时间天所以这两个被认为是相同的.
I want to compare 12/3/89 12:43:34
and 12/3/89 11:22:12
and have it disregard the actual time of day so both of these are considered the same.
我想我可以在比较之前将一天中的所有时间设置为 00:00:00
但实际上我确实想知道一天中的时间我也希望能够通过仅限日期.
I guess I can set all the times of day to 00:00:00
before I compare but I actually do want to know the time of day I just also want to be able to compare by date only.
我发现了一些具有相同问题的代码,它们分别比较了年、月和日.有没有更好的方法来做到这一点?
I found some code that has the same issue and they compare the year, month and day separately. Is there a better way to do this?
推荐答案
尝试使用 DateTime
对象上的 Date
属性...
try using the Date
property on the DateTime
Object...
if(dtOne.Date == dtTwo.Date)
....
这篇关于如何仅将 Linq 中的 DateTime 类型中的 Date 与 SQL with Entity Framework 进行比较?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!