2014-04-22 57 views
1

美好的一天!我需要改變加入到左加入我的查詢 -實體框架 - 內連接到左連接

 var query = (from sections in context.Sections 
        join themes in context.Themes on sections.SectionId equals themes.SectionId 
        join comments in context.Comments on themes.ThemeId equals comments.ThemeId 
        select new { sections.SectionId, sections.SectionTitle, themes.ThemeId, comments.CommentId } into x 
        group x by new { x.SectionId, x.SectionTitle } into g 
        select new SectionInfo 
        { 
         SectionId = g.Key.SectionId, 
         SectionTitle = g.Key.SectionTitle, 
         ThemeCount = g.Select(s => s.ThemeId).Count(), 
         CommentCount = g.Select(s => s.CommentId).Count() 
        }).ToList(); 

- 拜託,我不知道(

回答

2

您需要使用DefaultIfEmpty

一種方法是這樣的:

from themes in context.Themes.Where(x => sections.SectionId == x.SectionId) 
          .DefaultIfEmpty() 

替代方式

join themes in context.Themes on sections.SectionId equals themes.SectionId into themesGroup 
from themes in themesGroup.DefaultIfEmpty() 
+0

我必須替換哪部分查詢? – Skiminock

+0

@ user2953816,則需要替換要轉換的每個連接。在我的口袋裏,你需要替換 '在上下文中加入主題。部分.SectionId等於themes.SectionId'。爲你的其他加入做同樣的事情 – Aducci

+1

非常感謝你!有用! – Skiminock