使用多个联接、计数和左联接的SQL到LINQ
JOIN
LEFT JOIN
).
它给了我预期的结果.
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 cJOIN TABLE_V v on c.Id = v.IdLEFT JOIN ( SELECT Id, COUNT(*) AS cnt FROM TABLE_R GROUP BY Id) r ON c.Id = r.IdWHERE c.IdUser = '1234'GROUP BY c.Id, c.Title, r.cnt
var qResult = from c in dbContext.TABLE_C join v in dbContext.TABLE_V on c.IdC equals v.IdC join r in dbContext.TABLE_R on v.IdC equals r.IdC into temp from x in temp.DefaultIfEmpty() group x by new { c.IdC, c.Title /*miss something ?*/} into grouped select new { IdC = grouped.Key.IdC, --good result Title = grouped.Key.Title, --good result NbR = grouped.Distinct().Count(t => t.IdC > 0), --good, but "t.Id > 0" seems weird Count = --I'm lost. No idea how to get my COUNT(...) properties (Nb_V1 and Nb_V2) };
Count
相关分类