使用linq逐项比较两个列表 [英] Compare two lists, item by item, using linq
本文介绍了使用linq逐项比较两个列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想逐项比较两个列表.如何使用linq表达以下代码?
I want to compare two lists, item by item. How can I express the following code using 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
:
通过使用默认的相等比较器对其类型进行比较来确定两个序列是否相等.
Determines whether two sequences are equal by comparing the elements by using the default equality comparer for their type.
示例:
bool result = list1.SequenceEqual(list2);
这篇关于使用linq逐项比较两个列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文