troubleshooting Question

LINQ Let Statement - Determine Null Elements

Avatar of kruegerste
kruegersteFlag for United States of America asked on
.NET ProgrammingC#
9 Comments1 Solution2030 ViewsLast Modified:
Hello,

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(),
     }).ToList();

mySchedules.Add(docSportsInfo);
Console.WriteLine("Hello");

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
            }).ToList()
         ),
     } as object
    ).ToList();

mySchedules.Add(eventAndTeamInfo);

Open in new window

ASKER CERTIFIED SOLUTION
ericathome

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 9 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 9 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros