简体   繁体   English

LINQ查询仅在添加where子句后返回空引用

[英]LINQ query returns null reference only after adding where clause

I have 2 chained LINQ queries in a single method. 我在一个方法中有2个链接的LINQ查询。 The first takes a substring from table rows that have the form "Person XXXX-XXXX is marked as deleted" and extracts the XXXX-XXXX portion out and puts them into an object containing the XXXX-XXXX. 第一个从具有“人XXXX-XXXX被标记为已删除”格式的表行中获取子字符串,然后提取XXXX-XXXX部分并将其放入包含XXXX-XXXX的对象中。

var ids = from m in _repo.GetMessages()
                       where m.tool == 7
                       select new IDs
                       {
                           ID = m.text.Substring(6, 11),
                           text = m.text
                       };

This returns data as expected. 这将按预期返回数据。 The ids then joins to a subsequent "results" query: 然后,这些ID将加入到后续的“结果”查询中:

var results = from gs in _repo.GetSample()
                          join c in _repo.Getcenters() on gs.Iid equals c.Iid_c
                          join id in ids on gs.id equals id.ID
                          select new Results
                          {
                              c_id = c.id,
                              iid_d = gs.Iid_d,
                              Id = gs.id,
                              Num = gs.num,
                              sts_dt = c.sts_dt,
                              store_dte = gs.Store_dte,
                              quantity = gs.Quantity
                          };

The query in this form returns the data expected, though I need to add a where clause to the results query. 尽管我需要在结果查询中添加where子句,但这种形式的查询将返回预期的数据。 When I add a where clause that references any of the data objects in the query, the output produces "Object reference not set to an instance of an object." 当我添加引用查询中任何数据对象的where子句时,输出将产生“对象引用未设置为对象的实例”。 Without the where clause things look fine. 没有where子句,一切看起来很好。

My first thought was that null items existed in first dataset, so I added a != null clause and it had no effect. 我的第一个想法是第一个数据集中存在空项目,因此我添加了!= null子句,但没有任何效果。 Then I tried replacing the join with ID with a Contains(ID) statement and that didn't change things. 然后,我尝试用一​​个Contains(ID)语句替换ID为ID的联接,但这并没有改变。 Since I've written numerous linked LINQ queries before and chained them in this manner with no problem, my guess is that the join on the ID object may be causing something strange. 由于我之前已经编写了许多链接的LINQ查询,并以这种方式毫无问题地将它们链接起来,所以我的猜测是ID对象上的联接可能会引起一些奇怪的事情。 Does anyone have any ideas on how to add where clauses to this without producing that error? 有人对如何在其中添加where子句而不产生该错误有任何想法吗? Thank you! 谢谢!

After messing around with this for a while, I decided to try joining the 2 queries together and that seemed to fix the issue. 解决了一段时间后,我决定尝试将两个查询一起加入,这似乎可以解决问题。 Why I didn't do this to start with I'm not sure, but I'm guessing the join to an output of the previous query triggered a null object warning. 为什么不确定我为什么不这样做,但我猜对先前查询的输出的联接触发了空对象警告。 The following query works just fine: 以下查询可以正常工作:

var results = from gs in _repo.GetSample()
                      join c in _repo.Getcenters() on gs.Iid equals c.Iid_c
                      join ids in _repo.GetLogs() on gs.id equals ids.text.Substring(6,11)
                      where gs.quantity > 0
                      select new Results
                      {
                          c_id = c.id,
                          iid_d = gs.Iid_d,
                          Id = gs.id,
                          Num = gs.num,
                          sts_dt = c.sts_dt,
                          store_dte = gs.Store_dte,
                          quantity = gs.Quantity
                      };

It's also a much better and more compact query. 这也是一个更好,更紧凑的查询。 Thanks everyone! 感谢大家!

from gs in _repo.GetSample().Where(s=>s.quantity>0) ...尝试from gs in _repo.GetSample().Where(s=>s.quantity>0) ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM