문제

I'm trying to convert the following SQL query to LINQ,

SELECT 
C.CategoryId
, CategoryName 
,COUNT(DISTINCT I.ItemId)
FROM 
Categories  C
    LEFT JOIN  Items I
        ON I.CategoryId = C.CategoryId AND I.SubCategoryId = 1
GROUP BY C.CategoryId, C.CategoryName

Need all the categories even if the item count is 0.

도움이 되었습니까?

해결책

Using a group join, it should look something like this...

var result = Categories
                 .GroupJoin(
                     Items.Where(i => i.SubCategoryId == 1),
                     c => c.CategoryId, 
                     i => i.CategoryId,
                     (c, i) => new { c.CategoryId, c.CategoryName, i.Count() })
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top