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!
ContainsonFirstNameandLastNamebecause these properties can get null and cause your exception hereContactDynamicsResponse.Firstname.get returned null.. So Firstname is null and you are calling.Contains()on a string which is null so therefore you get aNullReferenceException