title: Getting NullReferenceException from .Select() when .Count() is greater than zero LINQ to XML
body: <p>I have an XML file:</p>
  
  <pre><code>&lt;Org href="https://vcloudserver/api/v1.0/org/272521719" type="application/vnd.vmware.vcloud.org+xml" name="blah-blah" xmlns="http://www.vmware.com/vcloud/v1"&gt;
       &lt;Link href="https://vcloudserver/api/v1.0/vdc/1093121285" type="application/vnd.vmware.vcloud.vdc+xml" name="blah-haha" rel="down"/&gt;
       &lt;Link href="https://vcloudserver/api/v1.0/vdc/1213262741" type="application/vnd.vmware.vcloud.vdc+xml" name="blah-hoho" rel="down"/&gt;
       &lt;Link href="https://vcloudserver/api/v1.0/tasksList/272521719" type="application/vnd.vmware.vcloud.tasksList+xml" rel="down"/&gt;
       &lt;Link href="https://vcloudserver/api/v1.0/catalog/1309520800" type="application/vnd.vmware.vcloud.catalog+xml" name="blah-hehe" rel="down"/&gt;
       &lt;Link href="https://vcloudserver/api/v1.0/org/272521719/catalog/1309520800/controlAccess/" type="application/vnd.vmware.vcloud.controlAccess+xml" rel="down"/&gt;
       &lt;Link href="https://vcloudserver/api/v1.0/org/272521719/catalog/1309520800/action/controlAccess" type="application/vnd.vmware.vcloud.controlAccess+xml" rel="controlAccess"/&gt;
       &lt;Link href="https://vcloudserver/api/v1.0/network/1435818199" type="application/vnd.vmware.vcloud.network+xml" name="blah-whodat" rel="down"/&gt;
       &lt;Link href="https://vcloudserver/api/v1.0/network/2048048931" type="application/vnd.vmware.vcloud.network+xml" name="blah-disis" rel="down"/&gt;
       &lt;Description/&gt;
       &lt;FullName&gt;Blah diddy Blah-Blah&lt;/FullName&gt;
  &lt;/Org&gt;
  </code></pre>
  
  <p>So, given the following:</p>
  
  <pre><code>XNamespace nameSpace = "http://www.vmware.com/vcloud/v1";
  var doc = XDocument.Parse(xml);
  </code></pre>
  
  <p>When I debug the following code in VS immediate window:</p>
  
  <pre><code>doc.Root.Elements(nameSpace + "Link").Count()
  </code></pre>
  
  <p>I get an expected value, 8.  But when I go a little further with things using:</p>
  
  <pre><code>var vdcs = doc.Root.Elements(nameSpace + "Link")
    .Select(x =&gt; new vDC()
    {
      Name = x.Attribute("name").Value,
      Type = x.Attribute("type").Value,
      Href = x.Attribute("href").Value
    }).Where(x=&gt;x.Type.Contains("vdc"));
  </code></pre>
  
  <p>I am getting a NullReferenceException trying to access vdcs.Count().  I have been messing with this for too long...  I have other places where this type of thing is working fine, so that doesn't help. :(  Tried stuffing a ToList() before the Where(), and that just move the NullReferenceException up to that call.</p>
  
  <p>If it helps, vDC is currently defined as:</p>
  
  <pre><code>public class vDC
  {
    public string Name { get; set; }
    public string Type { get; set; }
    public string Href { get; set; }
  }
  </code></pre>