使用多个联接、计数和左联接的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不过,我想要这个请求的Linq等价物,把它说成是我应用程序的数据访问层。我试过这样的方法: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在被攻击的子请求中。有人能解释我哪里错了吗?小贴士:如果有人能用lambda表达式写出等价物的话,那就加分了。
添加回答
举报
0/150
提交
取消