[英]Nullable object must have a value datetime using c# error
I am facing one problem in c# datetime i have trying more than time. 我在c#datetime中遇到一个问题,我尝试了不止时间。 it's not giving a problem solution.
它没有给出问题的解决方案。 so give me one solution.
所以给我一个解决方案。
var accommodationcategoryList = EmployeeAttendanceCacheMaster.GetAllEmployeeAttendance();
DataSourceResult result = accommodationcategoryList.ToDataSourceResult(request);
foreach(var item in accommodationcategoryList)
{
item.User = UserCacheMaster.GetUserById(item.employeeid);
item.loginTime = item.login.Value.ToShortDateString();
item.logoutTime = (item.logout.Value.ToShortDateString() != null) ? item.logout.Value.ToShortDateString() : "-";
}
My problem is not showing the logouttime it showing error looks like 我的问题不是显示注销时间,而是显示错误
nullable object must have a value
可为空的对象必须具有一个值
You're trying to check if a conversion against the null object is null rather than checking the nullable object itself. 您正在尝试检查针对null对象的转换是否为null,而不是检查可为空的对象本身。 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() : "-";
Try this: 尝试这个:
if(item.logout.HasValue) {
item.logoutTime = item.logout.Value;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.