代码之家  ›  专栏  ›  技术社区  ›  Perrier

多列上的EF Core Linq连接引发NullReference异常

  •  0
  • Perrier  · 技术社区  · 7 年前

    我有一个相当难看的查询,只需要再扩展一个连接。查询生成然后抛出NullReferenceException运行时。当我在处理异常细节时,我在 TargetSite/CustomAttributes/Message = "The method or operation is not implemented." 但我不知道用哪种方法?

    MarkedItems

    EF核心版本为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 回复  |  直到 7 年前
        1
  •  0
  •   Perrier    7 年前

    多亏了这些评论,我才发现了我的问题所在。我以前没有意识到,库存(I)也可以为null,所以我必须检查null,即使市场项目中的I-s(不仅仅是m-s)也可以。

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