EF Core Linq连接多个列会抛出NullReference异常

时间:2017-08-03 11:34:34

标签: c# entity-framework linq asp.net-core entity-framework-core

我有一个相当难看的查询,只需要再增加一个连接。然后,查询构建将抛出NullReferenceException运行时。当我在异常细节上挣扎时,我在TargetSite/CustomAttributes/Message = "The method or operation is not implemented."发现了这条消息,但我不知道哪种方法?

MarkedItems是新的连接,我认为问题可能是多列的连接,或者我必须将新表添加到group by子句中。同样的查询在LinqPad中使用EF6运行,所以这必须是尚未在EF7中实现的东西。

EF Core版本为1.1.2。

查询:

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

1 个答案:

答案 0 :(得分:0)

感谢您的评论,我能够找到查询的真正问题。我之前没有意识到,库存(i)也可能是空的,所以我不得不检查甚至是MarketItems join中的i-s(不仅是m-s)的空值。

不确定这对任何人是否有帮助,但在我遇到一些EF7 / EF6差异后,错误信息会产生误导。

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