使用EF或Linq

时间:2016-06-15 23:41:19

标签: sql-server entity-framework linq

我想知道如何用实体框架或linq计算中断持续时间和总和。我试着解释我的ms sql表。谢谢你的帮助。

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            |
+--------+--------------------------+---------------+----------------+

2 个答案:

答案 0 :(得分:0)

您可以在sql查询中使用lead并找到:

;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

答案 1 :(得分: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