I am trying to filter a list of objects using a multi field search box. It takes the text, then applies the search to the list. However, address 2 and 3 could potentially be blank (and in this case null in the objects the list is made up of) depending on the address submitted.
I have the following expression.
Properties.Where(x => x.Address1.ToLower().Contains(senderElement.Text)
|| x.Address2.ToLower().Contains(senderElement.Text)
|| x.Address3.ToLower().Contains(senderElement.Text))
.ToList();
Its currently throwing an exception because it, quite rightly so, can't search a null field.
How can i get it to skip or ignore null fields? Can it be done in a single expression?
You can add some null check like this
(x?.Address1?.ToLower()??"").Contains(senderElement?.Text??"")