Nullable object must have a value datetime using c# error

前端 未结 2 1479
伪装坚强ぢ
伪装坚强ぢ 2021-01-21 06:00

I am facing one problem in c# datetime i have trying more than time. it\'s not giving a problem solution. so give me one solution.

var accommodationcategoryList         


        
2条回答
  •  失恋的感觉
    2021-01-21 06:52

    You're trying to check if a conversion against the null object is null rather than checking the nullable object itself. You need to change this line:

    item.logoutTime = (item.logout.Value.ToShortDateString() != null) ? item.logout.Value.ToShortDateString() : "-";
    

    to

    item.logoutTime = item.logout.HasValue ? item.logout.Value.ToShortDateString() : "-";
    

提交回复
热议问题