【发布时间】:2018-08-14 06:33:37
【问题描述】:
有人可以解释它是如何工作的吗?我需要将 SQL Server 中的 DateTime 与用户输入进行比较。
这是我正在寻找的示例:
DateTime[] dates = { DateTime.Now, DateTime.UtcNow };
Console.WriteLine(dates.Where(x => x < DateTime.Now)); // I need to know if they are the same without converting them to strings.
Console.Read();
我正在使用实体框架,在实体类 Dates 中,属性是 DateTime,我需要将其与用户的输入进行比较,我找到的唯一解决方案是 CreateDate 函数。
如您所知,我无法将 POST 值直接与 Date 属性进行比较。示例:
db.dates.Where(x => x.StartDate < "2012-02-13 00:00:00.000") // error, cannot compare DateTime with string
所以,我尝试将值转换为 DateTime:
DateTime start_date = Convert.ToDateTime("2012-02-13 00:00:00.000");
db.dates.Where(x => x.StartDate < start_date ) // PROBLEM!!
为什么?那么start_date的输出是:
2012-02-13 12:00:00 am
但在数据库中,结束和开始日期如下:
2012-02-13 00:00:00.000 // all have zeros at the end
当输入日期为2012-02-13 00:00:00.000 格式时,如何比较日期?
我不在乎用什么,我只需要相同的格式和相同的数据类型。
【问题讨论】:
标签: c# entity-framework datetime