linq where 子句未产生预期结果
我有以下 xml:
<Root>
<Result img="1.png" name="a">
<Programs>
<Program name="foo1">
<ProgramID>1</ProgramID>
</Program>
</Programs>
</Result>
<Result img="2.png" name="b">
<Programs>
<Program name="foo1">
<ProgramID>1</ProgramID>
</Program>
<Program name="foo2">
<ProgramID>2</ProgramID>
</Program>
</Programs>
</Result>
<Result img="3.png" name="c">
<Programs>
<Program name="foo1">
<ProgramID>1</ProgramID>
</Program>
</Programs>
</Result>
<Result img="4.png" name="d">
<Programs>
<Program name="foo1">
<ProgramID>1</ProgramID>
</Program>
</Programs>
</Result>
</Root>
我尝试使用下面的 linq 语句按 ProgramID 过滤 xml,但当我传递值 2 时,我总是得不到任何结果,奇怪的是,当我传递值 1 时,我确实得到了预期结果,即所有四个结果。
xOut = New XElement("Root", _
From s In x...<Result> _
Where s.<Programs>.<Program>.<ProgramID>.Value = 2 _
Select s)
linq 查询出了什么问题。为什么 1 有效而 2 无效?我还想在过滤后保留 xml 结构。
I have the following xml:
<Root>
<Result img="1.png" name="a">
<Programs>
<Program name="foo1">
<ProgramID>1</ProgramID>
</Program>
</Programs>
</Result>
<Result img="2.png" name="b">
<Programs>
<Program name="foo1">
<ProgramID>1</ProgramID>
</Program>
<Program name="foo2">
<ProgramID>2</ProgramID>
</Program>
</Programs>
</Result>
<Result img="3.png" name="c">
<Programs>
<Program name="foo1">
<ProgramID>1</ProgramID>
</Program>
</Programs>
</Result>
<Result img="4.png" name="d">
<Programs>
<Program name="foo1">
<ProgramID>1</ProgramID>
</Program>
</Programs>
</Result>
</Root>
I am trying to filter xml by ProgramID with below linq statement but i always get no results back when i pass a value of 2, strangely when i pass a value of 1 in I do get expected results back which is all four results.
xOut = New XElement("Root", _
From s In x...<Result> _
Where s.<Programs>.<Program>.<ProgramID>.Value = 2 _
Select s)
What is wrong with linq query. Why does a 1 work but a 2 does not? I would also like xml structure preserved after filtering.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
下面是在 C# 中执行此操作的方法:
抱歉,不确定 VB.NET 的等效项是什么。
Here's how you'd do it in C#:
Not sure what the VB.NET equivalent would be though, sorry.