我用多个JOIN(包括一个LEFT JOIN)写了这个SQL请求.
它给了我预期的结果.
SELECT DISTINCT c.Id,
c.Title,
COUNT(v.Id) AS 'Nb_V2',
COUNT(DISTINCT v.IdUser) AS 'Nb_V1',
r.cnt AS 'Nb_R'
FROM TABLE_C c
JOIN TABLE_V v on c.Id = v.Id
LEFT JOIN (
SELECT Id, COUNT(*) AS cnt
FROM TABLE_R
GROUP BY Id
) r ON c.Id = r.Id
WHERE c.IdUser = '1234'
GROUP BY c.Id, c.Title, r.cnt
Run Code Online (Sandbox Code Playgroud)
但是,'Id喜欢Linq等同于此请求,将它放在我的应用程序的数据访问层.
我尝试过类似的东西:
var qResult = from c in dbContext.TABLE_C
join v in dbContext.TABLE_V on c.IdC equals v.IdC
join r in …Run Code Online (Sandbox Code Playgroud)