編輯:正如有人指出它不回答原來的問題,只是提供了一個替代方案。使用DateTime.Compare
https://msdn.microsoft.com/pl-pl/library/system.datetime.compare(v=vs.110).aspx
public static void Main()
{
DateTime date1 = new DateTime(2009, 8, 1, 0, 0, 0);
DateTime date2 = new DateTime(2009, 8, 1, 12, 0, 0);
int result = DateTime.Compare(date1, date2);
string relationship;
if (result < 0)
relationship = "is earlier than";
else if (result == 0)
relationship = "is the same time as";
else
relationship = "is later than";
Console.WriteLine("{0} {1} {2}", date1, relationship, date2);
}
在你的榜樣,也許這樣的
嘗試:
if(DateTime.Compare(item.BoughtDate.Value,DateTime.MaxValue) == 0) item.BoughtDate=null;
你需要將它轉換爲可空日期時間? 'item.BoughtDate =(DateTime?)null;' – Niklas
'BoughtDate'是否來自數據庫?用於記錄日期的精度差異可能會導致它們表面上看起來相同,但差別僅爲幾毫秒(通常爲3)。 –
'布爾結果= Nullable.Compare(DateTime.Now,yourDate)> 0;' –