简体   繁体   中英

LINQ query returns null reference only after adding where clause

I have 2 chained LINQ queries in a single method. 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.

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:

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. 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." Without the where clause things look fine.

My first thought was that null items existed in first dataset, so I added a != null clause and it had no effect. Then I tried replacing the join with ID with a Contains(ID) statement and that didn't change things. 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. Does anyone have any ideas on how to add where clauses to this without producing that error? 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) ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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