gpt4 book ai didi

c# - LINQ to XML - 根据与另一个 XElement 相比的节点值过滤 XElement?

转载 作者:太空宇宙 更新时间:2023-11-03 13:23:17 24 4
gpt4 key购买 nike

我想从 B 中选择 COMMISSION 元素,其中每个 COMMISSION 元素的 ID 值不等于 A COMMISSION 元素。

这是我完整的 Main 方法:

    static void Main(string[] args)
{
XElement A = XElement.Parse(
@"<CONTRACT>
<COMMISSION>
<ID>1</ID>
<PRODUCTS>
<PRODUCT>A</PRODUCT>
<PRODUCT>B</PRODUCT>
</PRODUCTS>
</COMMISSION>
<COMMISSION>
<ID>2</ID>
<PRODUCTS>
<PRODUCT>C</PRODUCT>
<PRODUCT>D</PRODUCT>
</PRODUCTS>
</COMMISSION>
<COMMISSION>
<ID>3</ID>
<PRODUCTS>
<PRODUCT>E</PRODUCT>
<PRODUCT>F</PRODUCT>
</PRODUCTS>
</COMMISSION>
</CONTRACT>");

XElement B = XElement.Parse(
@"<CONTRACT>
<COMMISSION>
<ID>3</ID>
<PRODUCTS>
<PRODUCT>E</PRODUCT>
<PRODUCT>F</PRODUCT>
</PRODUCTS>
</COMMISSION>
</CONTRACT>");

// This *should* only return COMMISSION elements from B.
// Rather, it returns the same B COMMISSION twice: once for the COMMISSION
// element (ID=3) in B, and once for the COMMISSION element (ID=3) in A.
var bOnly =
from excluded in A.Descendants("COMMISSION")
let included =
from b in B.Descendants("COMMISSION")
where b.Element("ID").Value != excluded.Element("ID").Value
select b
select new XElement("CONTRACT", included);

// Print results
foreach (var v in bOnly)
{
Console.WriteLine(v);
}

Console.ReadLine();
}

正如我在我的代码中所述,而不是获得所需的输出:

            <CONTRACT>
<COMMISSION>
<ID>3</ID>
<PRODUCTS>
<PRODUCT>E</PRODUCT>
<PRODUCT>F</PRODUCT>
</PRODUCTS>
</COMMISSION>
</CONTRACT>

我得到:

        <CONTRACT>
<COMMISSION>
<ID>3</ID>
<PRODUCTS>
<PRODUCT>E</PRODUCT>
<PRODUCT>F</PRODUCT>
</PRODUCTS>
</COMMISSION>
</CONTRACT>
<CONTRACT>
<COMMISSION>
<ID>3</ID>
<PRODUCTS>
<PRODUCT>E</PRODUCT>
<PRODUCT>F</PRODUCT>
</PRODUCTS>
</COMMISSION>
</CONTRACT>
<CONTRACT/>

我不知道为什么 COMMISSION w/ID=3 返回了两次。帮助!谢谢。请记住,ID 是我要比较的唯一元素值。如果 A 的 ID=3 w/与 B 的 ID=3 不同的产品,那么我希望我的结果不会显示任何内容。

我的最终解决方案:

           // Question: How do I only get COMMISSIONs from B that aren't in A?
// Requirement: I only want to compare ID between A and B COMMISSION sets,
// so if A ID=3 has products that are different from B ID=3, the query treats the
// two COMMISSIONs as the same so long as their ID is the same.
// Answer: (Steps 1 thru 3)

// Step 1) Get COMMISSIONs from B equalling COMMISSIONs from A on ID
var same =
from a in A.Descendants("COMMISSION")
join b in B.Descendants("COMMISSION")
on a.Element("ID").Value equals b.Element("ID").Value
select a;

// Step 2) Get all COMMISSIONs from B
var all = from b in B.Descendants("COMMISSION")
select b;

// Step 3) Get COMMISSIONs that are only in B
var different = all.Cast<XNode>().Except(same.Cast<XNode>(), new XNodeEqualityComparer());

// Print results
foreach (var v in different)
{
Console.WriteLine(v);
}

最佳答案

我已将您的 LINQ 查询更改为以下内容

var bOnly =
from a in A.Descendants("COMMISSION")
join b in B.Descendants("COMMISSION")
on a.Element("ID").Value equals b.Element("ID").Value
select a;

现在输出看起来不错,没有重复:

<COMMISSION>
<ID>3</ID>
<PRODUCTS>
<PRODUCT>E</PRODUCT>
<PRODUCT>F</PRODUCT>
</PRODUCTS>
</COMMISSION>

我还修复了您示例中的查询,它现在也可以正常工作:

var bOnly =
from b in B.Descendants("COMMISSION")
let same =
from a in A.Descendants("COMMISSION")
where a.Element("ID").Value == b.Element("ID").Value
select b
select new XElement("CONTRACT", same);

关于c# - LINQ to XML - 根据与另一个 XElement 相比的节点值过滤 XElement?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23369115/

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