-1

How to determine, if items in a List<List<int>> are equals ?

List<List<int>> equals = new List<List<int>>()
{
    new List<int>() { 1,2 },
    new List<int>() { 1,2 }
};

List<List<int>> notEquals = new List<List<int>>()
{
    new List<int>() { 1,2 },
    new List<int>() { 2,500}
};
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
BobyOneKenobi
  • 151
  • 1
  • 3
  • 10

1 Answers1

5

You need to compare the first list with all others, you can use SequenceEqual:

List<int> first = yourLists[0];
bool allEqual = yourLists.Skip(1).All(l => first.SequenceEqual(l));

Since All returns false on the first unequal list this is pretty efficient.

Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • Good. But isn't this works? `bool allEqual = first.SequenceEqual(yourLists[1]);` – Salah Akbari Dec 08 '15 at 11:50
  • @user2946329: if the list can only contain two sub-lists that works too. But my approach works even if it contains more than two. You are comparing only the first with the second list. What if there's a third list which is unequal? – Tim Schmelter Dec 08 '15 at 11:51
  • Right. So your answer is more comprehensive. – Salah Akbari Dec 08 '15 at 11:55