linq where 子句未产生预期结果

发布于 2024-12-14 14:43:33 字数 1555 浏览 2 评论 0原文

我有以下 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

赴月观长安 2024-12-21 14:43:33

下面是在 C# 中执行此操作的方法:

        var xOut = new XElement(
            "Root",
            x.Descendants("Result")
            .Where(y => y.Descendants("Programs").Descendants("Program").Descendants("ProgramID")
            .Any(z => z.Value == "2")));

抱歉,不确定 VB.NET 的等效项是什么。

Here's how you'd do it in C#:

        var xOut = new XElement(
            "Root",
            x.Descendants("Result")
            .Where(y => y.Descendants("Programs").Descendants("Program").Descendants("ProgramID")
            .Any(z => z.Value == "2")));

Not sure what the VB.NET equivalent would be though, sorry.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文