下面的代碼工作正常,可爲空值的日期時間:值不可爲空的System.DateTime
lastSync = syncHistsQuery.Max(m => m.Value);
爲什麼相同的代碼不是不可爲空的日期時間工作嗎?這是錯誤:
'System.DateTime' does not contain a definition for 'Value' and no extension method 'Value' accepting a first argument of type 'System.DateTime' could be found (are you missing a using directive or an assembly reference ?)
我是否必須將DateTime更改爲數據庫中的Nullable DateTime?是否有不同的方式來編寫不可爲空的DateTimes的代碼?
這裏是在情況下,整個功能它可以幫助:
public static DateTime GetMaxSyncDateTime()
{
DateTime lastSync = DateTime.MinValue;
using (var db = new CarbonContext())
{
var syncHistsQuery = from s in db.SyncHistories
select s.SyncHistDateTime;
lastSync = syncHistsQuery.Max(m => m.Value);
}
return lastSync;
}
試試看:'syncHistsQuery.Max();' – 2014-09-11 13:02:39