请帮我将下面的MSSQL转换为LINQ。
SELECT A.id, A.type, COUNT(IIf(B.BookId IS NOT NULL, 0, null)) AS TotalCount
FROM Store A
LEFT JOIN Book B ON A.id = B.id
GROUP BY A.id, A.type;
我目前有这个LINQ代码:
from a in Store
join b in Book on a.id equals b.id into c
from d in c.DefaultIfEmpty()
group a by new
{
a.id,
a.type
} into g
select new
{
StoreId = g.Key.id,
StoreType = g.Key.type,
TotalCount = g.Key !=null?g.Count():0
}
我想我错过了一些重要的事情。
答案 0 :(得分:1)
由于您没有尝试从联接的Book
部分提取任何值,因此您不需要DefaultIfEmpty()
,并且因为联接是“组加入”,因此into
子句,您不需要再次分组:
from a in Store
join b in Book on a.id equals b.id into c
select new
{
StoreId = a.id,
StoreType = a.type,
TotalCount = C.Count()
}