开发者

time portion from datetime value in LINQ

开发者 https://www.devze.com 2023-01-15 12:39 出处:网络
I have field which is of type DateTime. When inserting a record, I am also providing the time into the field.

I have field which is of type DateTime.

When inserting a record, I am also providing the time into the field.

When reading that field with LINQ, it will return me the correct date but the time values has the default, 12:00:00 AM

In the database, I have values with date and time for each record which is correct. The problem is that the time changes to default when reading/databinding with LINQ开发者_如何学Python.

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

What solution is there for extracting the time portion?


This is what is hurting you

AppointmentTime = q.AppointmentTime.Value.Date.TimeOfDay

When you do q.AppointmentTime.Value.Date it gets just the Date portion of the Value, by Default the TimeOfDay Property on a Date is 12:00:00 AM.

Instead you should do q.AppointmentTime.Value.TimeOfDay (this will only work if value is a type DateTime).

0

精彩评论

暂无评论...
验证码 换一张
取 消