Given something like this:
var results = theElement.Element("Blah").Element("Whatever").Elements("Something");
Is there an elegant way to deal with a null Blah or Whatever element so results is just null or empty in these cases?
I know I can split up the query and manually do these checks but was wondering if there was something more succinct.
You can add some extension methods to do this for you. For the
Elementmethod you would return null or the element itself. For theElementsmethod you would return an empty result or the target elements.These are the extension methods:
You can use them in this manner:
If you’re not querying for
ElementsOrEmptyand your last request is forElementOrDefaultyou would check for null instead of using theEnumerable.Anymethod.