title: LINQ to XML with join and GroupBy(). Is this correct?
body: <p>I am learning LINQ and this seems like a fairly simple problem.  I have a semi-working solution but I am sure it could be cleaned up.</p>
  
  <p>The propertyAuto node represents a car with id = 606.
  This node needs to have at least two child propertyValue nodes, one that references a vehicleValuation element with attribute of "Book" and one of "Auto". (My code doesnt check this yet, they could both be Auto or Book now)</p>
  
  <p>Finally, I need to get the Book value and Auto Value for the same car.</p>
  
  <p>Note that I will never know any IDs or idRef's beforehand and in the future there will be multiple cars.</p>
  
  <p>Here is my code now (ready to copy into LINQPad!)</p>
  
  <pre><code>var data = XElement.Parse (@"
  &lt;MyXML&gt;
    &lt;propertyAuto id='606'&gt;
      &lt;Values&gt;
        &lt;propertyValue idRef='f95d5dce-8152-4e9e-889e-7433d32664d6' /&gt;
        &lt;propertyValue idRef='cd1a83a7-dd04-41f9-b31c-5408a38ac777' /&gt;
      &lt;/Values&gt;
    &lt;/propertyAuto&gt;
    &lt;Valuations&gt;
      &lt;vehicleValuation id='cd1a83a7-dd04-41f9-b31c-5408a38ac777'
      valuationType='Auto' estimatedValue='8350.00' /&gt;
      &lt;vehicleValuation id='f95d5dce-8152-4e9e-889e-7433d32664d6'
      valuationType='Book' estimatedValue='12475.00' /&gt;
    &lt;/Valuations&gt;
  &lt;/MyXML&gt;");
  
  var valuations = from property in data.Descendants("propertyValue")
                   join value in data.Descendants("vehicleValuation")
                   on
                      (string)property.Attribute("idRef")
                   equals
                      (string)value.Attribute("id")
                   where property.Parent.Descendants("propertyValue").Count() &gt; 1
                   &amp;&amp; ((string)value.Attribute("valuationType") == "Auto" || (string)value.Attribute("valuationType") == "Book")               
                   select new { Value = value.Attribute("estimatedValue").Value, Type = value.Attribute("valuationType").Value, PropertyID = property.Parent.Parent.Attribute("id").Value };
  
  valuations.Dump();
  
  var values = valuations.GroupBy(x=&gt; x.PropertyID).FirstOrDefault();
  string auto = values.Where(x =&gt; x.Type == "Auto").Select(x=&gt;x.Value).First().ToString();
  string book = values.Where(x =&gt; x.Type == "Book").Select(x=&gt;x.Value).First().ToString();
  
  auto.Dump();
  book.Dump();
  </code></pre>
  
  <p>Is this workable or should I move to xpath, etc?</p>