Consider the following
var x= doc.Descendants("a").Where(p => p.Attributes["itemprop"].Value.Contains("image")).FirstOrDefault().Attributes["href"].Value;
Descendants might return null
Where might return null
Value might return null
Attributes might Be null etc..
is a try catch my only option to set the variable to null with the linq syntax to avoid the null reference exception, i simply want to set the variable to null and i don't want to split it out and use multiple if statements to check?
try
{
x= doc.Descendants("a").Where(p => p.Attributes["itemprop"].Value.Contains("image")).FirstOrDefault().Attributes["href"].Value;
}
catch
{
x=null;
}