实体框架-内部连接到左侧连接

本文关键字:连接 内部 框架 实体 | 更新日期: 2023-09-27 17:59:17

您好!我需要在我的查询中将联接转换为左联接

        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();

-拜托,我不知道(

实体框架-内部连接到左侧连接

您需要使用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()