2010-09-08 30 views
0

我有類型爲DateTime的字段。從LINQ的日期時間值的時間部分

當插入一條記錄時,我也會在該字段中提供時間。

當讀取場LINQ,它將返回我正確的日期,但時間值有默認,12:00:00 AM

在數據庫中,我有一個日期和時間爲每年這是正確的記錄值。問題在於,使用LINQ讀取/數據綁定時,時間會更改爲默認值。

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

有什麼解決方案來提取時間部分?

+0

你可以顯示你使用的Linq語句嗎? – 2010-09-08 13:01:37

+0

dataGridView1.DataSource = IQueriableObject.Select(q => new {AppointmentTime = q.AppointmentTime.Value.Date.TimeOfDay}); – Manish 2010-09-08 13:17:59

回答

6

這就是傷害你

AppointmentTime = q.AppointmentTime.Value.Date.TimeOfDay

當你做q.AppointmentTime.Value.Date它只是取值的日期部分,默認情況下TimeOfDay屬性上Date是12:00:00 AM。

取而代之,您應該做q.AppointmentTime.Value.TimeOfDay(只有在值爲DateTime時才能使用)。