views:

320

answers:

5

I get a null exception on this because MYTAG1 doesn't exist. I understand that this is because Element("MYTAG1") is null and calling Elements("MYTAG2") on it wont work.

How do I deal with this to prevent the crash?

     var myItems = from myNode in Nodes.Element("MYTAG1").Elements("MYTAG2")
                           select new EPTableItem
                           {
                           //    Assign stuff here                            
                           };
+2  A: 

I can't think of a clever way to incorporate an if statement into the C# query syntax, so I'll propose the following solution which checks for the required node before executing the query.

var myItems;
XElement myTag1 = myNode.Element("MYTAG1");

if (myTag1 != null)
{
    myItems = from myNode in myTag1.Elements("MYTAG2")  
              select new EPTableItem  
              {  
                  //    Assign stuff here                              
              };  
}
Steve Guidi
+1  A: 

One option is to define a new extension method and use that as your query source.

static IEnumerable<XElement> ElementAndChildren(this XElement parent, string name, string childName) 
{
    var element = parent.Element(name);
    if (element == null)
    {
        return Enumerable.Empty<XElement>();
    }
    return element.Elements(childName);
}

...
var myItems = from myNode in Nodes.ElementAndChildren("MYTAG1","MYTAG2")
                       select new EPTableItem
                       {
                       //    Assign stuff here                            
                       };
JaredPar
+1  A: 

I find that Linq is a lot easier when you use the extension methods instead of the pseudo sql syntax. You should be able to do something along these lines, but bare in mind that I have not tested the code.

var myItems = Nodes.Where(n => n.Element("MYTAG1") != null)
   .Select(n => n.Element("MYTAG1"))
   .Select(elem => elem.Elements("MYTAG2"))
   .Select(elem2 => new EPTTableItem { something = elem2.SomeProperty ... } );
klausbyskov
Wouldn't your second select return an IEnumerable<IEnumerable<T>>?
Rune FS
I was querying an XElement.Element() not nodes. it turned out that XElement didn't have the Where extension method
zachary
A: 

If you want to do it in LINQ something like this should work:

var myItems = from node in (from myNode in Nodes.Elements("MYTAG1")
              where myNode != null
              select myNode).SelectMany(x => x.Elements("MYTAG2"))
              select new EPTableItem
              {
                // Assign stuff here                            
              };

of course this will work differently if there's more occurrences of MYTAG

Rune FS
A: 

You can do it all in a single LINQ query with separate from clauses and a where clause.

var myItems = 
        from tag1 in Nodes.Elements("MYTAG1")
        where tag1 != null
        from tag2 in tag1.Elements("MYTAG2")
        select new EPTableItem
        {
        //    Assign stuff here                            
        };
Sam
This did not compile
zachary
zachary, I edited the code sample, it was missing an s on the first Elements.
Sam