0

我有一個相當醜陋的查詢,只需要擴展一個更多的聯接。查詢構建然後引發NullReferenceException運行時。由於我在例外的細節上掙扎,我在TargetSite/CustomAttributes/Message = "The method or operation is not implemented."找到了這條消息,但我不知道哪種方法?英孚核心Linq加入多列拋出NullReference異常

MarkedItems是新的連接,我認爲問題可能是多列連接,或者我不得不將新表添加到group by子句中。相同的查詢使用EF6在LinqPad中運行,所以這必須是EF7中尚未實現的東西。

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

嘗試用'i?.EANCode'和'i?.ProjectId'取代'i.EANCode'和'i.ProjectId'。 – dasblinkenlight

+0

很快,謝謝。一世?沒有工作,因爲類型不匹配,但'eancode =(i!= null?i.EANCode:string.Empty), projectid =(i!= null?i.ProjectId:Guid.Empty)'確實有效! – Perrier

+0

請包含** did **工作的查詢,以便我們可以比較。 – mjwills

回答

0

多虧了我能找到我的查詢,真正的問題的意見。我之前並沒有意識到,庫存(i)也可能爲空,因此我必須檢查MarketItems加入中的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);