Nullable object must have a value datetime using c# error

前端 未结 2 1473
伪装坚强ぢ
伪装坚强ぢ 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:43

    Try this:

    if(item.logout.HasValue) {
        item.logoutTime = item.logout.Value;
    }
    
    0 讨论(0)
  • 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() : "-";
    
    0 讨论(0)
提交回复
热议问题