"Value cannot be null" while creating an XElement from the attributes where one or more value is null

Viewed 1253

I am trying out following code:

XElement element = new XElement("ENTS", from i in notificationsTracking
select new XElement("ENT", 
new object[] {
    new XAttribute("ENTID", i.TrackingID),
    new XAttribute("PID", i.Response?.NotificationProvider),
    new XAttribute("UID", i.Response?.NotificationUniqueId)
}));

This works fine when the response is not null, and there exists values in "NotificationProvider" or "NotificationUniqueId" fields. But in case if any of these three is null then I am getting an error saying - "Value cannot be null".

I know there is one solution wherein I can explicitly compare the object/ properties against Null/ Empty and can convert them accordingly, and that will work.

But is there any optimized or more efficient way to solve this issue?

Thanks and Regards,

Nirman

1 Answers
Related