Handling Let Statement Nulls in LINQ to XML Query

Posted on 2010-03-30
Medium Priority
Last Modified: 2013-11-11
What is the best way to handle null Let statements in LINQ query?  

I see one could use the FirstOrDefault() keyword at the end to prevent error on a Let statement as such:

let personsMetaData = persons.Elements("player-metadata").FirstOrDefault()

But then inside the query when assigning values, it throws null reference cause personsMetaData is null.  

We are already checking if the Attribute itself is null as such:

Height = (personsMetaData.Attribute("height") == null) ? null : personsMetaData.Attribute("height").Value,

Do we need to also check if the element/variable from the Let statement is also null, as such:

Height = (personsMetaData == null) ? null : ((personsMetaData.Attribute("height") == null) ? null : personsMetaData.Attribute("height").Value),

Is there a better way than this?  Seems like a lot of code for every attribute or element value.  If this is the best option, can anybody show me an extender function that would make this easier to do?


List<PersonsSportsInfo> personsSportsInfo =
                    (from persons in Root.Descendants("player")
                     let personsMetaData = persons.Elements("player-metadata").First()
                     let firstName = (personsMetaData.Element("name") == null) ? "" : (personsMetaData.Element("name").Attribute("first") == null) ? "" : personsMetaData.Element("name").Attribute("first").Value
                     let lastName = (personsMetaData.Element("name") == null) ? "" : (personsMetaData.Element("name").Attribute("last") == null) ? "" : personsMetaData.Element("name").Attribute("last").Value
                     let careerPhase = persons.Descendants("career-phase")
                     select new PersonsSportsInfo
                         Key = (personsMetaData.Attribute("player-key") == null) ? null : personsMetaData.Attribute("player-key").Value,
                         UniformNumber = (personsMetaData.Attribute("uniform-number") == null) ? null : personsMetaData.Attribute("uniform-number").Value,
                         PositionRegular = (personsMetaData.Attribute("position-regular") == null) ? null : personsMetaData.Attribute("position-regular").Value,
                         Height = (personsMetaData.Attribute("height") == null) ? null : personsMetaData.Attribute("height").Value,
                         Weight = (personsMetaData.Attribute("weight") == null) ? null : personsMetaData.Attribute("weight").Value,
                         Status = (personsMetaData.Attribute("status") == null) ? null : personsMetaData.Attribute("status").Value,
                         DOB = (personsMetaData.Attribute("date-of-birth") == null) ? null : personsMetaData.Attribute("date-of-birth").Value,
                         Health = (personsMetaData.Attribute("health") == null) ? null : personsMetaData.Attribute("health").Value,
                         Caliber = (personsMetaData.Attribute(xts + "caliber") == null) ? null : personsMetaData.Attribute(xts + "caliber").Value,
                         FirstName = firstName,
                         LastName = lastName,
                         FullName = firstName + " " + lastName,
                         PositionDepth = (personsMetaData.Element("player-metadata-american-football") == null) ? null : (personsMetaData.Element("player-metadata-american-football").Attribute(xts + "position-depth") == null) ? "" : personsMetaData.Element("player-metadata-american-football").Attribute(xts + "position-depth").Value,
                         ProExperience = careerPhase
                             .Where(c => c.Attribute("phase-type").Value == "professional")
                             .Select(c => c.Attribute("duration").Value).FirstOrDefault(),
                         CollegeName = careerPhase
                             .Where(c => c.Attribute("phase-type").Value == "college")
                             .Select(c => c.Attribute("name").Value).FirstOrDefault(),

Open in new window

Question by:kruegerste
  • 2
LVL 64

Accepted Solution

Fernando Soto earned 2000 total points
ID: 29267845
Hi Steve;

In answer to the question, "Do we need to also check if the element/variable from the Let statement is also null, as such:", the simple answer is yes. This is because if the XElement does not exist then its attributes will also not exist and if the XElement does exist then it can still not have the needed attribute. This is a result of a dynamic xml document where it may contain any context and you got to guess which one's. The use of extension method can be a help here as well.


Author Closing Comment

ID: 31709072
Great info as usual, thanks. Hope you feel better.
LVL 64

Expert Comment

by:Fernando Soto
ID: 29297398
Not a problem, glad to help.

Thanks; I am feel better.  ;=)

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
How can you see what you are working on when you want to see it while you to save a copy? Add a "Save As" icon to the Quick Access Toolbar, or QAT. That way, when you save a copy of a query, form, report, or other object you are modifying, you…
Get the source code for a fully functional Access application shell with several popular security features that Access VBA application developers desire, but find difficult or impossible to figure out how to code. You get the source code for managi…

601 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question