gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 10:50:45 25 4
gpt4 key购买 nike

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com