Considering the following code
IEnumerable<String> query = null;
query = from x in xml.Descendants(xmlMasterContainerName).Descendants(xmlElementName)
let guid = x.Attribute("guid") ?? new XAttribute("guid", "-1")
where x.Attribute(xmlAttributeIdName).Value == xmlAttributeIdValue
select guid.Value;
I get the ‘object reference not set’ when trying query.ToList()
This is very probably caused by ‘select guid.Value’ when ‘x.Attribute(xmlAttributeIdName).Value == xmlAttributeIdValue’ does not exist.
How can I check the where statement for existing value before selecting?
Thanks
In XLinq, you usually don’t use
Attribute().Valuedirectly, because of the exact error you are getting. Instead, you cast it. The cast will result innullifAttribute()returned null, so there will be no exception.So, you would change your where clause to this:
and your select to this:
BTW: I would write that code like this: