【问题标题】:time portion from datetime value in LINQ来自 LINQ 中日期时间值的时间部分
【发布时间】:2011-04-09 17:38:23
【问题描述】:

我有日期时间类型的字段。

在插入记录时,我也在字段中提供时间。

使用 LINQ 读取该字段时,它将返回正确的日期,但时间值具有默认值,12:00:00 AM

在数据库中,每条记录的日期和时间都是正确的。问题是使用 LINQ 读取/数据绑定时,时间会更改为默认值。

dataGridView1.DataSource = IQueriableObject.Select(q => new 
         {AppointmentTime = q.AppointmentTime.Value.Date.TimeOfDay}) ;

提取时间部分有什么解决方案?

【问题讨论】:

  • 您能展示一下您使用的 Linq 语句吗?
  • dataGridView1.DataSource = IQueriableObject.Select(q => new {AppointmentTime = q.AppointmentTime.Value.Date.TimeOfDay}) ;

标签: winforms linq .net-3.5


【解决方案1】:

这就是伤害你的原因

AppointmentTime = q.AppointmentTime.Value.Date.TimeOfDay

当您执行 q.AppointmentTime.Value.Date 时,它只会获取值的日期部分,默认情况下,Date 上的 TimeOfDay 属性是 12:00:00 AM。

相反,您应该使用 q.AppointmentTime.Value.TimeOfDay(这仅在 value 是 DateTime 类型时才有效)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-08-03
    • 1970-01-01
    • 2014-05-22
    • 1970-01-01
    • 1970-01-01
    • 2010-09-05
    相关资源
    最近更新 更多