[英]compare two identical lists of strings
Let's say I have following code: 假设我有以下代码:
List<string> numbers = new List<string> { "1", "2" };
List<string> numbers2 = new List<string> { "1", "2"};
if (numbers.Equals(numbers2))
{
}
Like you can see I have two lists with identical items. 就像您看到的一样,我有两个包含相同项目的列表。 Is there a way to check if these two lists are equal by using one method?
有没有一种方法可以通过使用一种方法检查这两个列表是否相等?
SOLUTION: 解:
Use SequenceEqual()
使用
SequenceEqual()
Thanks 谢谢
使用Enumerable.SequenceEqual
,但首先对列表进行Sort
。
// if order does not matter
bool theSame = numbers.Except(numbers2).Count() == 0;
// if order is matter
var set = new HashSet<string>(numbers);
set.SymmetricExceptWith(numbers2);
bool theSame = set.Count == 0;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.