Perrier
Perrier

Reputation: 2837

EF Core Linq join on multiple columns throws NullReference Exception

I have a rather ugly query that just had to be expanded by one more join. The query builds then throws a NullReferenceException runtime. As I'm struggling around the exception details I found the message at TargetSite/CustomAttributes/Message = "The method or operation is not implemented." But I don't know which method?

MarkedItems is the new join and I think the problem could be the join on multiple columns or that I had to add the new table into the group by clause. The same query runs in LinqPad with EF6, so this must be something that hasn't been implemented in EF7 yet.

EF Core version is 1.1.2.

The query:

var inventory = (from it in _ctx.Items
                    join i in _ctx.Inventories on it.Id equals i.ItemId into iit
                    from i in iit.DefaultIfEmpty()
                    join m in _ctx.MarkedItems on 
                        new {
                                eancode = i.EANCode,
                                projectid = i.ProjectId
                            }
                        equals new {
                            eancode = (m != null ? m.EANCode : string.Empty),
                            projectid = (m != null ? m.ProjectId : Guid.Empty)
                        } into im
                    from m in im.DefaultIfEmpty()                    
                    where it.ProjectId == cmp.ProjectId
                    group i by new { 
                        EANCode = it.EANCode, 
                        ItemNo = it.ItemNo, 
                        Name = it.Name, 
                        BaseQty = it.BaseQty, 
                        Price = it.Price, 
                        m = (m != null ? m.EANCode : null)
                    } into lg
                    select new ComparisonBaseModel() {
                            EANCode = lg.Key.EANCode,
                            ItemName = lg.Key.Name,
                            Price = lg.Key.Price,
                            ScanQty = lg.Sum(s => s != null ? s.ScanQty : 0),
                            BaseQty = lg.Key.BaseQty,
                            DiffQty = lg.Sum(s => s != null ? s.ScanQty : 0) - lg.Key.BaseQty,
                            DiffPrice = lg.Key.Price * (lg.Sum(s=> s!= null ? s.ScanQty : 0) - lg.Key.BaseQty),
                            AllTasked = !lg.Any(s=>(s != null && s.InventoryTaskId == null) || s==null),
                            Flagged = lg.Key.m != null
                    }).Where(x=>x.DiffQty != 0);

Upvotes: 0

Views: 6317

Answers (1)

Perrier
Perrier

Reputation: 2837

Thanks to the comments I was able to find out the real problem with my query. I hadn't realized before, that Inventories (i) could be null, too, so I had to check for nulls even the i-s (not only m-s) in MarketItems join.

Not sure if this could be helpful to anyone but the error message was misleading after I have already run into some EF7/EF6 differences.

var inventory = (from it in _ctx.Items
                    join i in _ctx.Inventories on it.Id equals i.ItemId into iit
                    from i in iit.DefaultIfEmpty()
                    join m in _ctx.MarkedItems on 
                        new {
                                eancode = (i != null ? i.EANCode : string.Empty),
                                projectid = (i != null ? i.ProjectId : Guid.Empty)
                            }
                        equals new {
                            eancode = (m != null ? m.EANCode : string.Empty),
                            projectid = (m != null ? m.ProjectId : Guid.Empty)
                        } into im
                    from m in im.DefaultIfEmpty()                    
                    where it.ProjectId == cmp.ProjectId
                    group i by new { 
                        EANCode = it.EANCode, 
                        ItemNo = it.ItemNo, 
                        Name = it.Name, 
                        BaseQty = it.BaseQty, 
                        Price = it.Price, 
                        m = (m != null ? m.EANCode : null)
                    } into lg
                    select new ComparisonBaseModel() {
                            EANCode = lg.Key.EANCode,
                            ItemName = lg.Key.Name,
                            Price = lg.Key.Price,
                            ScanQty = lg.Sum(s => s != null ? s.ScanQty : 0),
                            BaseQty = lg.Key.BaseQty,
                            DiffQty = lg.Sum(s => s != null ? s.ScanQty : 0) - lg.Key.BaseQty,
                            DiffPrice = lg.Key.Price * (lg.Sum(s=> s!= null ? s.ScanQty : 0) - lg.Key.BaseQty),
                            AllTasked = !lg.Any(s=>(s != null && s.InventoryTaskId == null) || s==null),
                            Flagged = lg.Key.m != null
                    }).Where(x=>x.DiffQty != 0);

Upvotes: 2

Related Questions