2016-06-15 97 views
1

我想知道如何計算休息時間和總計與實體框架或linq。我試圖解釋我的MS SQL表。感謝您的幫助。用EF或Linq計算休息時間的持續時間

enter image description here

+---------+-------------------------+---------------+----------------+ 
| EventId | EventDurations   | EventName | AgentNumber | 
+---------+-------------------------+---------------+----------------+ 
| 1  | 2016-06-06 09:51:21.570 | Login   | 801   | 
| 2  | 2016-06-06 09:51:33.083 | Break   | 801   | 
| 3  | 2016-06-06 13:58:14.617 | Login   | 801   | 
| 4  | 2016-06-06 15:30:50.600 | Break   | 801   | 
| 5  | 2016-06-06 15:46:32.837 | Login   | 801   | 
| 6  | 2016-06-06 15:47:16.777 | Break   | 801   | 
+--------+--------------------------+---------------+----------------+ 
+0

請同時將數據發佈到我們可以複製並粘貼到我們的開發環境的表單中。然後我們可以編碼並測試答案。 – Enigmativity

回答

0

您可以在SQL查詢中使用鉛和發現:

;with cte as (
select *, datediff(mi,EventDurations, lead(EventDurations,1,EventDurations) over(partition by EventName order by EventDurations)) dur from EventTable 
) 
select EventName, sum(dur) from cte group by EventName 

它仍然不是EF或LINQ

+0

我正在使用sql server 2008,並且沒有導致內置函數 –

+0

您可以檢查這個嗎? –

0

你能查嗎?沒有內建的引導/延遲

;WITH CTE as 
(
    SELECT RN = ROW_NUMBER() OVER (PARTITION BY EventName ORDER BY EventDurations), * 
    FROM EventsTable 
) 
SELECT [Cur_row].*, ISNULL(DATEADD(DAY, -1, [Next_Row].EventDurations), '31-Dec-2099') AS EndDate 
FROM CTE [Cur_row] LEFT JOIN CTE [Next_Row] 
ON [Cur_row].EventId = [Next_Row].EventId AND [Next_Row].RN = [Cur_row].RN + 1 
ORDER BY [Cur_row].RN;--if require add eventid