使用 Linq 检查集合中具有特定值的所有项目是否都在另一个集合中?

发布于 2024-11-29 00:02:52 字数 641 浏览 0 评论 0原文

我知道您可以检查一个列表中的所有元素是否存在于另一个列表中,如下所示(来自此处的另一篇文章):

bool result = list.All(x => dataList.Contains(x));

但是如何检查另一个列表中存在的所有元素是否具有相同的“子值”?

我有两个 xml 文档 _mapdoc_mapdocCopy,我想比较所有在其中找到属性路径的 元素其他 xml 文档。

所以我开始做与上面类似的事情:

if (_mapdocCopy.Descendants("file").All(x => _mapdoc.Descendants("file").Contains(x)))

但我立即意识到这会比较整个 元素,并且它们通常不会相同(它们可能有不同的子元素),甚至如果它们的“path”属性具有相同的值。我只想测试这一点。

我也(在不同的上下文中)希望能够获得不相同的(仍然仅涉及路径属性),因此也将不胜感激。但这是另一个问题,我仍然需要上面的 Linq 查询。

有什么想法吗?

I know you can check if all elements from one list exist in another list like this (from another post here):

bool result = list.All(x => dataList.Contains(x));

But how do I check if all elements exist in another list have the same "subvalue"?

I have two xml documents _mapdoc and _mapdocCopy, and I want to compare all <file> elements where the attribute path are all found in the other xml document.

So I started doing something similar to above:

if (_mapdocCopy.Descendants("file").All(x => _mapdoc.Descendants("file").Contains(x)))

But I immediately realized that this would compare the entire <file> elements, and they will often not be the same (they may have different sub elements), even if their "path" attribute have the same values. And it's only this I want to test for.

I also (in a different context) want to be able to get the one's that are not the same (still with regards to the path attribute only), so help with that would also be appreciated. But it's a different question, I still need the Linq query above.

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

孤独患者 2024-12-06 00:02:53

我认为将 .Contains 替换为 .Any(适当的条件) 。像这样的东西:

if (_mapdocCopy.Descendants("file").All(
        x => _mapdoc.Descendants("file").Any(y => y.path == x.path)))

编辑:广告第二部分,蛮力方法是:(

... = _mapdocCopy.Descendants("file").Where(
        x => !_mapdoc.Descendants("file").Any(y => y.path == x.path)))

采用其他集合中不存在的那些 - 请注意,闭包被否定)。不过还有 except linq 方法。我不确定我是否有正确的语法,但它会是这样的:(

... = _mapdocCopy.Descendants("file").Except(
        _mapdoc.Descendants("file"),
        (x, y) => x.path == y.path)

除非我不确定闭包是否会转换为 IEqualityComparer 或者需要一些额外的黑客技术)

I'd think along the lines of replacing the .Contains with .Any(appropriate condition). Something like:

if (_mapdocCopy.Descendants("file").All(
        x => _mapdoc.Descendants("file").Any(y => y.path == x.path)))

Edit: Ad second part, the brute force approach is:

... = _mapdocCopy.Descendants("file").Where(
        x => !_mapdoc.Descendants("file").Any(y => y.path == x.path)))

(take those that don't exist in the other set -- note that the closure is negated). However there is also Except linq method. I am not sure I've got correct syntax, but it would be something like:

... = _mapdocCopy.Descendants("file").Except(
        _mapdoc.Descendants("file"),
        (x, y) => x.path == y.path)

(except where I am not sure whether the closure will convert to IEqualityComparer or needs some extra hackery for it)

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