开发者

How to cast to System.Timespan?

can someone tell me how to cast System.Timespan? to System.Timespan I keep getting开发者_Go百科 this error when tryin to get the date difference between the current date and a date from a linq query (see belwo)

System.TimeSpan ts = i.joinDt - DateTime.Now.Date;


System.TimeSpan ts = (i.joinDt - DateTime.Now.Date).Value;


To get a TimeSpan from a TimeSpan? you need to access the Value property of the nullable - no need to cast.

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

Or:

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


can someone tell me how to cast System.Timespan? to System.Timespan

You need to specify a default value for the case where the TimeSpan? is null:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜