-2

I have an object where a property may exist or may not exist.

if(response.AddressInformation.AddressResponses.Any(inf => inf.AddressResponse.matchCodeStatus.ToLower().Equals("usps_match")))
{

}

I have two array items of AddressResponse. First item has null for matchCodeStatus and thats where I get object not set to an instance exception. How can I achieve my target and escape this exception?

I tried to put a null check before my IF, but it didnt work

if(response.AddressInformation.AddressResponses.Any(inf => inf.AddressResponse.matchCodeStatus != null)
Huma Ali
  • 1,564
  • 4
  • 29
  • 57

1 Answers1

0

As of C# 6, you can also use a null conditional operator ?. :

inf => inf.AddressResponse.matchCodeStatus?.ToLower().Equals("usps_match"))
Thinh
  • 243
  • 2
  • 11