0

I know that there are several SO posts on this matter and I have used them when coming up with my current solution.

Looking at the 4 rows of code below, the first three are not working but the forth one is (!?) and I do not understand why that is. The first two only checks against a single string: "Where values that contains 'string'", and the later two checks values against a list of strings "Where values that contain any of the strings in this list"

contactsDynamicResponses.Values.Where(cdr => cdr.Firstname.Contains(firstName)).ToList();
contactsDynamicResponses.Values.Where(cdr => cdr.Lastname.Contains(lastName)).ToList();

contactsDynamicResponses.Values.Where(cdr => accountIdsList.Any(ail => cdr.AccountId.Contains(ail))).ToList();
contactsDynamicResponses.Values.Where(cdr => contactIdsList.Any(cil => cdr.ContactId.Contains(cil))).ToList();

My goal is to filter the list depending on the search filters entered into the function. Below is the error I get, which makes me think there is something about the contactsDynamicResponses.Values that is the problem here, but I am lost.

System.NullReferenceException: 'Object reference not set to an instance of an object.' Project.Entities.Contact.Models.Dynamics.ContactDynamicsResponse.Firstname.get returned null.

How the rows look completely:

contactsDynamicResponses.Values = string.IsNullOrEmpty(firstName) ? contactsDynamicResponses.Values : contactsDynamicResponses.Values.Where(cdr => cdr.Firstname.Contains(firstName)).ToList();
contactsDynamicResponses.Values = string.IsNullOrEmpty(lastName) ? contactsDynamicResponses.Values : contactsDynamicResponses.Values.Where(cdr => cdr.Lastname.Contains(lastName)).ToList();
           
contactsDynamicResponses.Values = accountIdsList == null ? contactsDynamicResponses.Values : contactsDynamicResponses.Values.Where(cdr => accountIdsList.Any(ail => cdr.AccountId.Contains(ail))).ToList();
contactsDynamicResponses.Values = contactIdsList == null ? contactsDynamicResponses.Values : contactsDynamicResponses.Values.Where(cdr => contactIdsList.Any(cil => cdr.ContactId.Contains(cil))).ToList();

Any thoughts are welcome! Thank you!

2
  • 2
    Seems you should add null-checks before calling Contains on FirstName and LastName because these properties can get null and cause your exception here Commented Mar 22, 2021 at 13:02
  • 2
    The exception already tells you exactly what the problem is: ContactDynamicsResponse.Firstname.get returned null.. So Firstname is null and you are calling .Contains() on a string which is null so therefore you get a NullReferenceException Commented Mar 22, 2021 at 13:04

2 Answers 2

1

Add null-checks in the query:

contactsDynamicResponses.Values.Where(cdr => cdr.Firstname != null && 
    cdr.Firstname.Contains(firstName)).ToList();

The exception says that FirstName is null.
Also add this check in the other queries, for example for LastName

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

1 Comment

Aaah, of course! It didn't occur to me that cdr.FirstName could be null (even though it was spelled out). Thank you for making my day :)
0

A shorter way to do a null-check:

contactsDynamicResponses.Values.Where(cdr =>
    cdr.Firstname?.Contains(firstName) == true
).ToList();

It looks a bit odd with the == true but that is because the result of the comparison is bool?

Comments

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.