2015-11-03 29 views
0

如何選擇具有Timespan類型的計算列的記錄,我只想選擇Timespan大於某個數量的記錄?Linq查詢選擇的時間跨度小於「x」

MyRecords.Where(e=>e.Hours>0) 

小時是從結束時間柱減去開始時間計算。 以上代碼返回:

運算符'>'不能應用於'System.TimeSpan?'類型的操作數和 'INT'

回答

0

嘗試:

MyRecords.Where(e=>e.Hours != null && e.Hours.Value.Milliseconds > 0) 
0

什麼:

MyRecords.Where(e => e.Hours.HasValue && e.Hours.Value.Hours > 0); 
3

你不能直接比較TimeSpan對象爲int,需要在使用的屬性之一TimeSpan:https://msdn.microsoft.com/en-us/library/system.timespan(v=vs.110).aspx

例如MyRecords.Where(e => e.Hours.HasValue && e.Hours.Value.Milliseconds > 0)

e.Hours.HasValuee.Hours.Value需要由於E實際上是TimeSpan?類型是可空類型

我會建議重新命名計算列e.Hours爲更具說明性的,因爲它是代表TimeSpan?不只是幾個小時的。