-1

请帮我将下面的 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
}

我想我错过了一些重要的东西。

4

1 回答 1

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()
}
于 2013-05-20T18:42:44.650 回答