We help IT Professionals succeed at work.
Get Started

LINQ Let Statement - Determine Null Elements

kruegerste asked
Last Modified: 2013-11-11

Does anybody know how to check for null elements in a Let statement?  In the code below, there are many Let statements inside the queries and i would like to code these to where it doesn't fail if element is missing.

For the element and attribute value checks I'm using an inline if statement to check for null first on the attribute or element object.  But this doesn't work for a Let statement using Elements("").FIrst()  

For example, I need to make sure the "sports-metadata" element exists before assigning to SportsMetaData.  Additionally, if the element doesn't exist, I'm not sure how that affects values that I try to assign values to using SportsMetaData.  Side note: In most cases, there is only one element with the name but I'm still using elements("  ").First(), not sure if that is wrong or not, it works.

 let SportsMetadata = listing.Elements("sports-metadata").First()

Any ideas? Thanks.
// This is needed when accessing an XElement or Attribute that has a namespace on its name
XNamespace xts = "http://www.xmlteam.com";

List<Object> mySchedules = new List<object>();
XElement Root = xmlDoc.Root;
List<DocumentSportsInfo> docSportsInfo = 
    (from listing in xmlDoc.Descendants("sports-content")
     let SportsMetadata = listing.Elements("sports-metadata").First()
     let SportsContextCode = listing.Descendants("sports-content-code")
     select new DocumentSportsInfo
         //QueryDateTime = myFodHelper.ConvertFodDateTime(Root.Attribute("query-date-time").Value),
         QueryString = Root.Attribute("query-string").Value,
         HostName = Root.Attribute("hostname").Value,
         ResultCount = Convert.ToInt32(Root.Attribute("result-count").Value),
         ErrorCount = Convert.ToInt32(Root.Attribute("error-count").Value),
         TotalCount = Convert.ToInt32(Root.Attribute("total-count").Value),
         ElapsedTime = Root.Attribute("elapsed-time").Value,
         DocumentId = SportsMetadata.Attribute("doc-id").Value,
         //DocumentDateTime = myFodHelper.ConvertFodDateTime(SportsMetadata.Attribute("date-time").Value),
         DocumentClass = SportsMetadata.Attribute("document-class").Value,
         FixtureKey = SportsMetadata.Attribute("fixture-key").Value,
         RevisionId = SportsMetadata.Attribute("revision-id").Value,
         DocumentSportsTitle = SportsMetadata.Element("sports-title").Value,
         Priority = SportsContextCode
                    .Where(c => c.Attribute("code-type").Value == "priority")
                    .Select(c => c.Attribute("code-key").Value).FirstOrDefault(),
         Sport = SportsContextCode
                 .Where(c => c.Attribute("code-type").Value == "sport")
                 .Select(c => c.Attribute("code-key").Value).FirstOrDefault(),
         League = SportsContextCode
                 .Where(c => c.Attribute("code-type").Value == "league")
                 .Select(c => c.Attribute("code-key").Value).FirstOrDefault(),
         Conference = SportsContextCode
                 .Where(c => c.Attribute("code-type").Value == "conferenece")
                 .Select(c => c.Attribute("code-key").Value).FirstOrDefault(),
         TeamKeys = SportsContextCode
                 .Where(c => c.Attribute("code-type").Value == "team")
                 .Select(c => c.Attribute("code-key").Value).ToList(),


List<object> eventAndTeamInfo =
    (from sEvent in Root.Descendants("sports-event")
     select new List<object>
         new EventSportsInfo 
             Key = sEvent.Element("event-metadata").Attribute("event-key").Value, //event-status
             Status = sEvent.Element("event-metadata").Attribute("event-status").Value,
             Week = Int32.Parse(sEvent.Element("event-metadata").Element("event-metadata-american-football").Attribute(xts + "week").Value),
             // Add the others here
         new List<TeamsSportsInfo>  
            (from t in sEvent.Descendants("team")
            // The let's are here so that you do not have to write them 4 times and can
            // be used short string and can combine them as in FullName
            let FN = t.Element("team-metadata").Element("name").Attribute("first").Value
            let LN = t.Element("team-metadata").Element("name").Attribute("last").Value
            select new TeamsSportsInfo
                // Add fileds here
                Key = t.Element("team-metadata").Attribute("team-key").Value,                                                         
                FirstName = FN,
                LastName = LN,
                FullName = FN + " " + LN
                // Add more fileds here
     } as object


Open in new window

Watch Question
This problem has been solved!
Unlock 1 Answer and 9 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE