我想找到Xelement attribute.value,其中子级具有具体的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);
<Assembly name="1">
<Assembly name="44" />
<Assembly name="3">
<Component name="2" />
</Assembly>
</Assembly>
最佳答案
由于嵌套Where
子句的编写方式。
内部子句为
child.Descendants("Component").Where(name => name.Attribute("name").Value==item)
IEnumerable<XElement>
,因此外部子句读取.Where(child => /* an IEnumerable<XElement> */)
Where
需要一个Func<XElement, bool>
类型的参数,在这里您最终要传递Func<XElement, IEnumerable<XElement>>
-因此出错。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();
关于c# - 无法将类型 'IEnumerable<XElement>'隐式转换为 'bool',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11032724/