Your problem is that Descendents
and Where
return an IEnumerable<XElement>
not a single XElement
which is what you're after. You can fix this like this:
XElement toEdit = doc.Descendants("ArrayOfCustomer")
.Descendants("Customer")
.Where(x => Guid.Parse(x.Descendants("CustomerId").Single().Value) == customer.CustomerId)
.FirstOrDefault();
I would restructure your query like this:
XElement toEdit = doc.Descendants("Customer")
.Where(x => (Guid)x.Element("CustomerId") == customer.CustomerId)
.FirstOrDefault();
You are not casting x
you are casting x.Descendants()
. x.Descendants() returns a collection, hence the plural method semantic. Off the top of my head you should be able to do x.Descendants("CustomerId").FirstOrDefault() as XElement
XElement toEdit = (from c in doc.Descendants("Customer")
where Guid.Parse(c.Value) == customer.CustomerId
select c).SingleOrDefault();