我正在尝试在 LINQ to SQL 中编写以下查询。该表包含用户安排的会话列表,查询计算每个用户的连续会话之间的平均时间量。它使用左连接,以便只有一个会话的用户具有NULL
值。
SELECT t1.workerId, AVG(DATEDIFF(s, t1.endTime, t2.startTime))
FROM e_userLongSessions t1
LEFT JOIN e_userLongSessions t2
ON t1.workerId = t2.workerId AND t1.sessionNum = t2.sessionNum - 1
GROUP BY t1.workerId
ORDER BY t1.workerId
基于问题LINQ to SQL Left Outer Join和How to do joins in LINQ on multiple fields in single join,我得到了以下查询:
from s1 in gzClasses.e_userLongSessions
join s2 in gzClasses.e_userLongSessions
on new {w = s1.workerId, n = s1.sessionNum} equals new {w = s2.workerId, n = s2.sessionNum - 1}
into joined
from s2 in joined.DefaultIfEmpty(null)
group new {s1, s2} by s1.workerId into g
select g.Average(e => e.s2 == null ? (double?) null : (e.s2.startTime - e.s1.endTime).TotalSeconds);
我收到一条Unsupported overload used for query operator 'DefaultIfEmpty'
消息。有什么建议么?