-1

How Can I check null for each object I am using in below chain?

forensicId =  Message.Events.SMS.SMS_Mappings.FirstOrDefault().Bug.ForensicId;

More details: I want to access ForensicId from (tables/Proxies loaded by entity framework) BUG which is part of a SMS_Mappings and SMS_Mappings are again part of some table.

Is there any way where I can check if Message is not null or of events are not null and SMS is not null and so on within a single line.

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Sweetie
  • 1,298
  • 6
  • 24
  • 48

1 Answers1

2

Try this forensicId = Message?.Events?.SMS?.SMS_Mappings?.FirstOrDefault()?.Bug?.ForensicId; It returns null if any object in chain is null or ForensicId value if everything is ok. There is a nice article about such scenarios

Pavel Anikhouski
  • 21,776
  • 12
  • 51
  • 66