LINQ to Entities causing exception in .NET Core, "Value cannot be null"

c# entity-framework-core linq

Question

I'm currently working on trying to convert our companies framework from EF6 to be compatible with EF Core. I've run into a bit of a block. An EF "stored procedure" that works just fine on EF6 is now failing on this block of code in EF Core:

var allFolderAncestors = (from f in context.MENU_MenuFolders
                          from mtf in context.MENU_MenuToolbar_MenuFolders
                                             .Where(x => x.MenuFolderId == f.Id 
                                                      || x.MenuFolderId == f.ParentFolderId)
                                             .DefaultIfEmpty()
                          where (toolbarId == -1 
                          || (mtf == null 
                                ? false 
                                : mtf.MenuToolbarId == toolbarId)
                          ) 
                          && f.Id != 0
                          select new
                          {
                              AncestorFolderId = f.Id,
                              AncestorParentFolderId = f.ParentFolderId,
                              Id = f.Id,
                              ParentFolderId = f.ParentFolderId
                          }).ToList();

Trying to execute this line of code results in the following exception message:

Value cannot be null. Parameter name: left

In our .NET Core solution, this code DOES work when the input parameter toolbarId is set to -1. So my guess is the problem lies somewhere in the other side of the OR clause. And that's where I'm stuck. I was wondering if anyone has seen this problem before and knows how to fix it? Or how EF Core differs from EF6? I've tried several different fixes and looked in several places for a solution and have come up empty.

1
4
3/7/2017 5:07:01 PM

Accepted Answer

Well, did a little bit more tinkering with it and found out what the issue was.

   var allFolderAncestors = (from f in context.MENU_MenuFolders
                             from mtf in context.MENU_MenuToolbar_MenuFolders
                                                .Where(x => x.MenuFolderId == f.Id 
                                                     || x.MenuFolderId == f.ParentFolderId)
                                                .DefaultIfEmpty()
                             where (toolbarId == -1 
                             || (mtf != null && mtf.MenuToolbarId == toolbarId)) 
                             && f.Id != 0
                             select new
                             {
                                 AncestorFolderId = f.Id,
                                 AncestorParentFolderId = f.ParentFolderId,
                                 Id = f.Id,
                                 ParentFolderId = f.ParentFolderId
                             }).ToList();

In the where clause after the OR, apparently .NET Core does not like the ternary operator. Hope this helps anyone else that may run into this issue.

4
3/7/2017 4:44:05 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow