【问题标题】:entity framework - inner join to left join实体框架 - 内连接到左连接
【发布时间】:2014-04-22 14:23:52
【问题描述】:

美好的一天!我需要在查询中将连接转换为左连接 -

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

-拜托,我不知道(

【问题讨论】:

    标签: c# sql linq entity-framework join


    【解决方案1】:

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

    【讨论】:

    • 我必须替换查询的哪一部分?
    • @user2953816,您需要替换每个要转换的联接。在我的回答中,您需要替换join themes in context.Themes on sections.SectionId equals themes.SectionId。为您的其他加入做同样的事情
    猜你喜欢
    • 2011-07-29
    • 1970-01-01
    • 2013-09-19
    • 2021-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多