无法将类型“IEnumerable”隐式转换为“bool”
本文关键字:转换 bool XElement 类型 IEnumerable | 更新日期: 2023-09-27 18:37:14
我想找到Xelement attribute.value,其中子具有具体的属性.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);
如何获取属性值?它说什么是布尔值?
编辑最初我有以下 XML:
<Assembly name="1">
<Assembly name="44" />
<Assembly name="3">
<Component name="2" />
</Assembly>
</Assembly>
我需要获取属性值,其中其子项(XElement)具有特定的属性值在这个例子中,我会得到字符串"3",因为我正在搜索属性.value == "2" 的子项的父级
因为嵌套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();
我想你想要
string fatherName = xmlNX.Descendants("Assembly")
.Where(child => child.Elements("Component").Any(c => (string)c.Attribute("name") == item))
.Select(el => (string)el.Attribute("name")).FirstOrDefault();