I have the following XML document:
<?xml version="1.0" encoding="UTF-8"?>
<FamilyTree>
<Parent name="Ken">
<Child name="Lorna">
<Grandchild name="Andrew"/>
<Grandchild name="Brian"/>
</Child>
<Child name="Mike">
<Grandchild name="Ann"/>
<Grandchild name="Beth"/>
</Child>
</Parent>
<Parent name="Norma">
<Child name="Owen">
<Grandchild name="Charles"/>
</Child>
<Child name="Peter">
<Grandchild name="Charlotte"/>
</Child>
</Parent>
<Parent name="Quinn">
<Child name="Robert">
<Grandchild name="Debbie"/>
<Grandchild name="Eric"/>
</Child>
<Child name="Susan">
<Grandchild name="Frank"/>
</Child>
</Parent>
<Parent name="Tom">
<Child name="Ursula">
<Grandchild name="George"/>
<Grandchild name="Harriet"/>
</Child>
<Child name="Victor">
<Grandchild name="Ian"/>
<Grandchild name="Juliet"/>
</Child>
</Parent>
</FamilyTree>
I'm trying to select all the "Parents" with a Child who has at least two children ("Grandchild") of his/her own. Note that I'm not looking for "Parents" with at least two "Grandchild[ren]".
The following LINQ query works, but I've a feeling it's not the most elegant.
IEnumerable<XElement> parents = (from c in familyTreeElement.Descendants("Child")
where c.Elements().Count() > 1
select c.Parent).Distinct();
Is there a better way to specify this?