[英]Converting TimeSpan to milliseconds in linq query
我正在嘗試使用實體框架在linq查詢中將TimeSpan
添加到DateTime
。 我有以下代碼:
編輯
TimeSlots
是一個ICollection<TimeSlot>
TimeSlot
具有屬性DateTime Start
和TimeSpan Duration
var linq = from s in schedulesBase
join c in channelBase on s.ChannelId equals c.ChannelId
select new
{
...
TimeSlots = s.TimeSlots,
...
};
var timeslots = linq.SelectMany(t => t.TimeSlots);
return from s in timeslots
select new Resources.Event
{
...
end = DbFunctions.AddSeconds((DateTime?)s.Start, (Int32?)s.Duration.Seconds)
...
};
它可以很好地編譯,但是從s.Duration.Seconds
到end
都不會增加任何時間。 由於以下代碼有效
end = DbFunctions.AddSeconds((DateTime?)s.Start, 500)
並表現出預期,我從TimeSpan
到秒的轉換一定做錯了。
在第一種情況下,我在做什么錯?
我懷疑問題是您使用的是Seconds
而不是TotalSeconds
。 Seconds
屬性僅提供[-59, 59]
范圍內的值-因此5分鍾30秒將返回30,而不是330。
嘗試:
end = DbFunctions.AddSeconds((DateTime?)s.Start, (Int32?)s.Duration.TotalSeconds)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.