Reputation: 423
I want to find the Xelement attribute.value which children have a concrete attribute.value.
string fatherName = xmlNX.Descendants("Assembly")
.Where(child => child.Descendants("Component")
.Where(name => name.Attribute("name").Value==item))
.Select(el => (string)el.Attribute("name").Value);
How can I get the attribute.value? What does it say that is a bool?
EDITED Originally I Have the following XML:
<Assembly name="1">
<Assembly name="44" />
<Assembly name="3">
<Component name="2" />
</Assembly>
</Assembly>
I need to get the attribute.value where its children (XElement) has an expecific attribute.value In this example, I would get the string "3" because i am searching the parent of the child which attribute.value == "2"
Upvotes: 0
Views: 4228
Reputation: 437574
Because of how the nested Where
clauses are written.
The inner clause reads
child.Descendants("Component").Where(name => name.Attribute("name").Value==item)
This expression has a result of type IEnumerable<XElement>
, so the outer clause reads
.Where(child => /* an IEnumerable<XElement> */)
However Where
expects an argument of type Func<XElement, bool>
and here you end up passing in a Func<XElement, IEnumerable<XElement>>
-- hence the error.
I 'm not offering a corrected version because your intent is not clear at all from the given code, please update the question accordingly.
Update:
Looks like you want something like this:
xmlNX.Descendants("Assembly")
// filter assemblies down to those that have a matching component
.Where(asm => asm.Children("Component")
.Any(c => c.name.Attribute("name").Value==item))
// select each matching assembly's name
.Select(asm => (string)asm.Attribute("name").Value)
// and get the first result, or null if the search was unsuccessful
.FirstOrDefault();
Upvotes: 2
Reputation: 167696
I think you want
string fatherName = xmlNX.Descendants("Assembly")
.Where(child => child.Elements("Component").Any(c => (string)c.Attribute("name") == item))
.Select(el => (string)el.Attribute("name")).FirstOrDefault();
Upvotes: 1