LINQ 左连接可空列给出空引用异常

LINQ Left join on nullable column giving null reference exception

我正在处理一个 linq 查询,它有一个涉及可为 null 的列的左连接。此连接是在具有 int Id 列的 BackgroundColors table 和 table 之间完成的名为 MenuFolders,其中有一个名为 BackgroundColorIdint? 列。在 MenuFolders 中,每一行的 BackgroundColorId 都设置为 null

我查询中的每个左联接都可以正常工作,直到这两个 table 上的联接为止。当我取消注释 backgroundColors left join with menuFolders 时,查询生成 NullReferenceException - “Object reference not set to an instance of an object.” 但我认为 .DefaultIfEmpty() 应该照顾好那个。这是我的代码。请记住,当 运行 on SQL Server:

时,SQL 等效项工作正常
  var folderStructure = (from fa in folderAncestorsLanguage
                         from mf in menuFolders.Where(x => x.Id == fa.Id)
                         from mtf in menuTlbrMenuFolders.Where(x => (x.MenuToolbarId == toolbarId && x.MenuFolderId == fa.Id)).DefaultIfEmpty()
                         from mbc in backgroundColors.Where(x => x.Id == mf.BackgroundColorId).DefaultIfEmpty()//Left Join that is causing an exception
                         from lmf in languageMenuFolders.Where(x => x.MenuFolderId == mf.Id).DefaultIfEmpty()
                         where (mf.StatusId == 1)
                         select new
                         {
                             Id = mf.Id,
                             Name = lmf.Name,
                             DefaultName = mf.Name,
                             Description = mf.Description,
                             FolderId = fa.ParentFolderId,
                             OrderIndex = mf.OrderIndex,
                             IconUrl = mf.IconUrl,
                             IsFramework = mf.IsFramework,
                             BackgroundColor = mbc.HexCode == null ? null : mbc.HexCode,
                             IsModifiable = mf.IsModifiable,
                             iconCls = mf.iconCls
                         }).ToList();  

我也试过使用标准 linq 而不是 lambda 表达式来执行此查询,但它仍然给我同样的错误。

我之前查看了这个 link 以帮助回答这个问题,但它的答案对我不起作用: LINQ Join query (with nullable ref between table)

编辑:我尝试将 BackgroundColorId 列中的值从 null 更改为整数值,但我仍然遇到相同的错误。

var folderStructure = (from fa in folderAncestorsLanguage
                     from mf in menuFolders.Where(x => x.Id == fa.Id)
                     from mtf in menuTlbrMenuFolders.Where(x => (x.MenuToolbarId == toolbarId && x.MenuFolderId == fa.Id)).DefaultIfEmpty()
                     from mbc in backgroundColors.Where(x => x.Id == mf.BackgroundColorId).DefaultIfEmpty()//Left Join that is causing an exception
                     from lmf in languageMenuFolders.Where(x => x.MenuFolderId == mf.Id).DefaultIfEmpty()
                     where (mf.StatusId == 1)
                     select new
                     {
                         Id = mf.Id,
                         Name = (lmf == null) ? null : lmf.Name,
                         DefaultName = mf.Name,
                         Description = mf.Description,
                         FolderId = fa.ParentFolderId,
                         OrderIndex = mf.OrderIndex,
                         IconUrl = mf.IconUrl,
                         IsFramework = mf.IsFramework,
                         BackgroundColor = (mbc == null) ? null : mbc.HexCode,
                         IsModifiable = mf.IsModifiable,
                         iconCls = mf.iconCls
                     }).ToList();

我最终弄明白了,我的一些连接导致表虽然不为空,但我引用的 为空,因此导致了异常.希望这对 运行 解决此问题的其他人有所帮助。