linq-to-xml - 检查 IEnumerable<XAtrribute> 类型列表中的值是否存在于另一个相同类型 + LINQ to XML 列表中

标签 linq-to-xml xattribute

如何检查 Xrclist 中是否存在 XTclist 的值。

XR:

<result>
 <claims type="Subject">
          <scope_of_claim>Full scope</scope_of_claim>
          <claim_date>02/28/2009</claim_date>
          <claim_age months="1" years="2" />
</claims>
 <claims type="Vehicle">
          <scope_of_claim>Full scope</scope_of_claim>
          <claim_date>12/8/2010</claim_date>
          <claim_age months="1" years="2" />
</claims>

XT:

<result>
   <claims type="Vehicle">
          <scope_of_claim>Full scope</scope_of_claim>
          <claim_date>24/1/2011</claim_date>
          <claim_age months="2" years="0" />
   </claims>
</result>

代码:

var XRclist = XR.Descendants("claims").Attributes("type");
    var xTclist = XT.Descendants("claims").Attributes("type");

         foreach (var c in xTclist)
         {
             if (XRclist.Contains(c.value)) // This line need to be corrected
             {
                Do some thing.
             }
             else
             {
               Do something else.
             }
         }

最佳答案

您可以使用扩展方法 Any:

而不是if (XRclist.Contains(c.value))

使用if (XRclist.Any(x => x.Value.Equals(c.Value))

关于linq-to-xml - 检查 IEnumerable<XAtrribute> 类型列表中的值是否存在于另一个相同类型 + LINQ to XML 列表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6232810/

相关文章:

c# - 简洁的 LINQ to XML 查询

c# - 排除子元素

c# - 如何删除 XMLDocument 中的特定属性?

c# - 添加 xml :space to root element

c# - XAttribute 生成虚假前缀

c# - 使用 xmlns 属性( namespace )查询 XDocument

c# - 使用 LINQ to XML,如何根据序号位置连接两组数据?

linq-to-xml - 使用 XNamespace 创建格式良好的 XML

c#-4.0 - 如何在XElement中添加更多属性?