[英]Calculate difference between DateTime.Now and time(7) value in C#?
我有一個數據庫,其中包含員工的時間表,反映他們應該何時進出時鍾。 例如,當他們打卡時,我想將當前時間與他們計划的時間進行比較。 對於我的一生,我似乎無法解決。 我正在這樣檢索預定時間:
if (day=="Friday")
{
var auth = from d in dc.Dailies
where d.Number == employee
select d.FridayStart; ;
}
看起來不錯,但我嘗試的所有示例都沒有。...一個可能的問題似乎是,盡管我將計划時間存儲為time(7)
,但查詢似乎將它們作為TimeSpan
值返回,這使我感到困惑。
好吧,根據您的代碼,您可以
if (day=="Friday")
{
//auth is expected to be an IQueryable, which derives from IEnumerable,
//so it could be many results
var auth = from d in dc.Dailies
where d.Number == employee
select d.FridayStart;
//So, if you're expecting only one result, and guarantee that will be one,
//you can use .First() or .Single()
var timeDifference = auth.First() - DateTime.Now.TimeOfDay;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.