假设我有以下 SQL 语句:
SELECT p.ParentId, COUNT(c.ChildId)
FROM ParentTable p
LEFT OUTER JOIN ChildTable c ON p.ParentId = c.ChildParentId
GROUP BY p.ParentId
如何将此转换为LINQ to SQL?我在 COUNT(c.ChildId) 上卡住了,生成的 SQL 总是输出 COUNT(*)。以下是我的代码:
from p in context.ParentTable
join c in context.ChildTable on p.ParentId equals c.ChildParentId into j1
from j2 in j1.DefaultIfEmpty()
group j2 by p.ParentId into grouped
select new { ParentId = grouped.Key, Count = grouped.Count() }
谢谢!