C#:比较两个IEnumerables的内容 [英] C#: Compare contents of two IEnumerables
本文介绍了C#:比较两个IEnumerables的内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否可以使用内置的linq方法查找两个序列是否包含相同的项目,而不考虑顺序?
Is there a built in linq method thing I can use to find out if two sequences contains the same items, not taking the order into account?
例如:
{1, 2, 3} == {2, 1, 3}
{1, 2, 3} != {2, 1, 3, 4}
{1, 2, 3} != {1, 2, 4}
您拥有SequenceEquals,但随后我必须先订购两个序列,对吗?
You have the SequenceEquals, but then I would have to Order both sequences first, wouldn't I?
推荐答案
有很多方法.假设A和B是IEnumerable.
There are quite a few ways. Assume A and B is IEnumerable.
!A.Except(B).Any() && !B.Except(A).Any()
A.Count() == B.Count() && A.Intersect(B).Count() == B.Count()
etc
这篇关于C#:比较两个IEnumerables的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文