1

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!

3
  • 2
    Can you provide an example of a statement with a where clause that causes problems? Commented Jun 25, 2015 at 21:27
  • hope you are using the Where clause before the Select... Commented Jun 25, 2015 at 22:02
  • Yes, I am putting the where clause between the join and select. An example of a statement that does this (every statement I've tried causes the error): where gs.quantity > 0 Commented Jun 26, 2015 at 20:53

2 Answers 2

1

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!

Sign up to request clarification or add additional context in comments.

Comments

0

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

1 Comment

That gave me the same result, unfortunately.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.