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.
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)
Finally, I need to get the Book value and Auto Value for the same car.
Note that I will never know any IDs or idRef's beforehand and in the future there will be multiple cars.
Here is my code now (ready to copy into LINQPad!)
var data = XElement.Parse (@"
<MyXML>
<propertyAuto id='606'>
<Values>
<propertyValue idRef='f95d5dce-8152-4e9e-889e-7433d32664d6' />
<propertyValue idRef='cd1a83a7-dd04-41f9-b31c-5408a38ac777' />
</Values>
</propertyAuto>
<Valuations>
<vehicleValuation id='cd1a83a7-dd04-41f9-b31c-5408a38ac777'
valuationType='Auto' estimatedValue='8350.00' />
<vehicleValuation id='f95d5dce-8152-4e9e-889e-7433d32664d6'
valuationType='Book' estimatedValue='12475.00' />
</Valuations>
</MyXML>");
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() > 1
&& ((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=> x.PropertyID).FirstOrDefault();
string auto = values.Where(x => x.Type == "Auto").Select(x=>x.Value).First().ToString();
string book = values.Where(x => x.Type == "Book").Select(x=>x.Value).First().ToString();
auto.Dump();
book.Dump();
Is this workable or should I move to xpath, etc?