[英]Compare two lists, item by item, using linq
I want to compare two lists, item by item. 我想逐项比较两个列表。 How can I express the following code using linq?
如何使用linq表达以下代码?
bool result = true;
var list1 = new List<int> { 10, 20, 30, 40 };
var list2 = new List<int> { 10, 20, 30, 40 };
for (int index = 0; index < list1.Count(); index++)
{
result &= list1[index] == list2[index];
}
You can use SequenceEqual
: 您可以使用
SequenceEqual
:
Determines whether two sequences are equal by comparing the elements by using the default equality comparer for their type.
通过使用元素的默认相等比较器比较元素,确定两个序列是否相等。
Example: 例:
bool result = list1.SequenceEqual(list2);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.