Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/visual-studio-2010/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何转换到System.Timespan?_C#_Visual Studio 2010 - Fatal编程技术网

C# 如何转换到System.Timespan?

C# 如何转换到System.Timespan?,c#,visual-studio-2010,C#,Visual Studio 2010,有人能告诉我如何施展System.Timespan吗?到系统。时间跨度 当我试图从linq查询中获取当前日期和某个日期之间的日期差时,我一直遇到这个错误(请参见belwo) 要从TimeSpan?中获取TimeSpan,您需要访问nullable的属性-无需强制转换 TimeSpan? tsn = i.joinDt - DateTime.Now.Date; TimeSpan ts; if(tsn.HasValue) { ts = tsn.Value; } 或: 有人能告诉我如何施展Syst

有人能告诉我如何施展System.Timespan吗?到系统。时间跨度 当我试图从linq查询中获取当前日期和某个日期之间的日期差时,我一直遇到这个错误(请参见belwo)


要从
TimeSpan?
中获取
TimeSpan
,您需要访问nullable的属性-无需强制转换

TimeSpan? tsn = i.joinDt - DateTime.Now.Date;
TimeSpan ts;
if(tsn.HasValue)
{
  ts = tsn.Value;
}
或:

有人能告诉我如何施展System.Timespan吗?到系统。时间跨度

如果
TimeSpan?
为空,则需要指定默认值:

TimeSpan? nullableTs = ...
TimeSpan ts = nullableTs ?? TimeSpan.Zero;

我建议
System.TimeSpan ts=I.joinDt.Value-DateTime.Now.Date
以避免将
DateTime.Now.Date
转换为可为空的值,仅用于再次转换结果。
if(i.joinDt.HasValue)
{
  TimeSpan ts = i.joinDt.Value - DateTime.Now.Date;
}
System.TimeSpan ts = (i.joinDt - DateTime.Now.Date).Value;
TimeSpan? nullableTs = ...
TimeSpan ts = nullableTs ?? TimeSpan.Zero;