Reputation: 589
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];
}
Upvotes: 1
Views: 1548
Reputation: 101162
You can use 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);
Upvotes: 8