views:

35

answers:

1

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.

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

What solution is there for extracting the time portion?

+5  A: 

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).

msarchet