C# 在ToLower之前检查任何空对象

C# 在ToLower之前检查任何空对象,c#,linq,nullreferenceexception,any,C#,Linq,Nullreferenceexception,Any,我有一个对象,其中一个属性可能存在,也可能不存在 if(response.AddressInformation.AddressResponses.Any(inf => inf.AddressResponse.matchCodeStatus.ToLower().Equals("usps_match"))) { } 我有两个AddressResponse数组项。第一项matchCodeStatus为null,这就是我获取对象未设置为实例异常的地方。我怎样才能实现我的目标并摆脱这个例外 我试

我有一个对象,其中一个属性可能存在,也可能不存在

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

}
我有两个AddressResponse数组项。第一项matchCodeStatus为null,这就是我获取对象未设置为实例异常的地方。我怎样才能实现我的目标并摆脱这个例外

我试图在IF之前加上一个空检查,但没有用

if(response.AddressInformation.AddressResponses.Any(inf => inf.AddressResponse.matchCodeStatus != null)

从C 6开始,还可以使用空条件运算符:


您的标题解释了如何在尝试调用实例方法(如ToLower)之前检查空值。@NatPongjardenlarp检查我编辑的inf=>inf.AddressResponse.matchCodeStatus!=null&&inf.AddressResponse.matchCodeStatus.ToLower.EqualSups_match?@stickybit噢,我的错,没有在正确的位置放置null检查。它起作用了
inf => inf.AddressResponse.matchCodeStatus?.ToLower().Equals("usps_match"))