LINQ:确定两个序列中包含完全相同的元件 [英] LINQ : Determine if two sequences contains exactly the same elements

查看:96
本文介绍了LINQ:确定两个序列中包含完全相同的元件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要确定两套是否包含完全相同的元素。该排序并不重要。

I need to determine whether or not two sets contains exactly the same elements. The ordering does not matter.

例如,这两个数组应该是平等的考虑:

For instance, these two arrays should be considered equal:

IEnumerable<int> data = new []{ 3,5,6,9 };
IEnumerable<int> otherData = new []{ 6,5,9,3}

一组不能包含任何元素,不在其他。

One set cannot contain any elements, that are not in the other.

可以这样做使用内置的查询操作?并且这将是最有效的方式来实现它,考虑到元件的数目的范围可以从几个到几百个?

Can this be done using the built-in query operators ? And what would be the most efficient way to implement it, considering that the number of elements could range from a few to hundreds ?

推荐答案

如果你想治疗的数组作为套,而忽略秩序和重复的项目,你可以使用的 的HashSet&LT; T&GT; .SetEquals 方法

If you want to treat the arrays as "sets" and ignore order and duplicate items, you can use HashSet<T>.SetEquals method:

var isEqual = new HashSet<int>(first).SetEquals(second);

否则,你最好的选择可能是分拣两个序列以同样的方式,并使用 SequenceEqual 进行比较。

这篇关于LINQ:确定两个序列中包含完全相同的元件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆