title: Extract all the elements that meet the condition from an XELEMENT var + LINQ to XML
body: <p>I have two XELEMENTS <code>XR</code> and <code>XTemp</code>. </p>
  
  <p>MY <code>XR</code> Contains :</p>
  
  <pre><code>     &lt;result xmlns="http://abc.com/test"&gt;
             &lt;report&gt;
              &lt;unit unit_number="1" id="S1"&gt;
                   &lt;classification&gt;Subject&lt;/classification&gt;
                   &lt;claim_reported count="0" status="No claims reported" /&gt;
              &lt;/unit&gt;
             &lt;unit unit_number="2" id="S2"&gt;
                  &lt;classification&gt;Subject&lt;/classification&gt;
                  &lt;claim_reported count="0" status="No claims reported" /&gt;
             &lt;/unit&gt;
          &lt;unit unit_number="3" id="V1"&gt;
            &lt;classification&gt;Vehicle&lt;/classification&gt;
            &lt;claim_reported count="1" status="Claims reported" /&gt;
          &lt;/unit&gt;
      ......
       &lt;/report&gt;
  &lt;/result&gt;
  </code></pre>
  
  <p>My <code>XTemp</code> contains :</p>
  
  <pre><code> &lt;result xmlns="http://abc.com/test"&gt;
     &lt;report&gt;
          &lt;unit unit_number="1" id="S3"&gt;
               &lt;classification&gt;Subject&lt;/classification&gt;
               &lt;claim_reported count="1" status="Claims reported" /&gt;
          &lt;/unit&gt;
         &lt;unit unit_number="2" id="S4"&gt;
              &lt;classification&gt;Subject&lt;/classification&gt;
              &lt;claim_reported count="4" status="Claims reported" /&gt;
         &lt;/unit&gt;
        &lt;unit unit_number="3" id="V2"&gt;
           &lt;classification&gt;Vehicle&lt;/classification&gt;
           &lt;claim_reported count="0" status="No claims reported" /&gt;
        &lt;/unit&gt;
        &lt;unit unit_number="3" id="V3"&gt;
           &lt;classification&gt;Vehicle&lt;/classification&gt;
           &lt;claim_reported count="2" status="Claims reported" /&gt;
        &lt;/unit&gt;
           ......
      &lt;/report&gt;
   &lt;/result&gt;
  </code></pre>
  
  <p>Trying to extract all the <code>&lt;unit&gt;</code> elements from <code>XTemp</code> based on a certain condtion and add them to <code>XR</code>.</p>
  
  <p>The condition is : get all the <code>&lt;unit&gt;</code> elements whose id  begins with "V".</p>
  
  <p>Below code was the best I was able to come up with. </p>
  
  <p>But, I get <code>Object reference not set to an instance</code> Error.</p>
  
  <pre><code>XR.Descendants(xmlns + "unit").LastOrDefault().AddAfterSelf(XTemp.Descendants(xmlns + "unit")
    .Where(x =&gt; x.Element(xmlns + "unit").Attribute("id").Value.ToUpper().Contains("V")));
  </code></pre>
  
  <p>Thanks in advance</p>
  
  <p>BB</p>