这是xml的第一个查询:
var doc = XDocument.Parse(p.text);
var texts = doc.Descendants("Text")
.Where(e => (string)e.Attribute("section") == SectionName)
.Select(e => new Text
{
AudioList = (string)e.Attribute("audio"),
Content = (string)e.Value,
Group_Name = (string)e.Attribute("group")
})
.Distinct();
并且想要在第二个中使用查询结果
var audio = XDocument.Parse(p.audio);
var audios = audio.Descendants("Audio")
.Where(
(e => e.Attribute("group") == (texts.Where(dr => dr.Group_Name)))
&&
(e => e.Attribute("File_name")) == (texts.Where(dr => dr.AudioList))
)
.Select(e => new Audio
{
Path = (string)e.Attribute("Path")
})
.Distinct();
XML:
<Texts>
<Text group="Outbuilding0">blank</Text>
<Text group="Study0" audio="abc.wav" section="Walls and skirting">[[Walls and skirting]] </Text>
<Text group="Study0" audio="c.wav" section="Walls and skirting">[[Walls and skirting]] </Text>
</Texts>
<Audio group="Outbuilding0">
<File_name>2013042517364073_Outbuilding0_1.wav</File_name>
<Path>/Job_Files/74/2_Outbuilding0/Audio/2013042517364073_Outbuilding0_1.wav</Path>
<Size>32</Size>
<Audio_length>00:00:04</Audio_length>
</Audio>
但是显示can not convert implicitly from string to bool
在条件中的错误......这是正确的方法吗?
答案 0 :(得分:1)
您的错误来自
Where(dr => dr.Group_Name)
。
您需要为where子句提供要评估的布尔值。你想要像
这样的东西 .Where(dr => dr.Group_Name == e.Group_Name)
我不太确定你之后是什么,但是;
XDocument document = XDocument.Load("data.xml");
var texts = from t in document.Descendants("Text")
select new
{
AudioList = t.Attribute("audio") != null ? t.Attribute("audio").Value : string.Empty,
Content = t.Value,
Group_Name = t.Attribute("group") != null ? t.Attribute("group").Value : string.Empty
};
var audio = from a in document.Descendants("Audio")
let groupName = a.Attribute("group") != null ? a.Attribute("group").Value : string.Empty
let fileName = a.Element("File_name") != null ? a.Element("File_name").Value : string.Empty
let t = texts.Where(t => t.Group_Name == groupName && t.AudioList == fileName)
where t.Any()
select new
{
Path = a.Element("Path").Value
};
您需要做的是验证文本列表是否包含您之后的音频条目。我认为这是您的查询尝试做的事情。显然,如果一切都空了,你会得到一个结果(我不确定你对模式做了什么假设)