可空对象必须具有使用C#错误的值datetime [英] Nullable object must have a value datetime using c# error

查看:76
本文介绍了可空对象必须具有使用C#错误的值datetime的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在c#datetime中遇到一个问题,我尝试了不止时间。不是
提供问题解决方案。因此,请给我一个解决方案。

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 = 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


推荐答案

您正在尝试检查针对null对象的转换是否为null,而不是检查可为null的对象本身。您需要更改以下行:

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() : "-";

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

这篇关于可空对象必须具有使用C#错误的值datetime的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆