c# - 无法将类型 'IEnumerable'隐式转换为 'bool'

我想找到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); 

如何获取attribute.value?这是什么意思?

编辑过
最初,我具有以下XML:
<Assembly name="1">
  <Assembly name="44" />
  <Assembly name="3">
     <Component name="2" />
  </Assembly>
  </Assembly>

我需要获取其子项(XElement)具有特定的attribute.value的attribute.value
在此示例中,我将获取字符串“3”,因为我正在搜索child.attribute.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();

关于c# - 无法将类型 'IEnumerable<XElement>'隐式转换为 'bool',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11032724/

相关文章:

winforms - C++ CLI KeyDown::raise 错误 error C3767 候

objective-c - 运行奇怪的错误运行 objective-c 代码

c++ - C++模板泛型(模板参数列表)

android - 从现有代码创建新项目

c# - 从LINQ查询返回匿名类型?

boost - 使用复合键从 multi_index_container 中删除每个键的元素

c# - 通用 - 编译器警告 CS0693 或错误

f# - 编译错误提示值不是函数

c# - 'System.Data.Objects.ObjectContext'不包含带有0个参数的

node.js - 编译 Node 给出 “No-old-style-declaration”标志错