score:19

Accepted answer

You can use SequenceEqual with additional order:

return list1.OrderBy(x => x).SequenceEqual(list2.OrderBy(x => x));

score:5

You can use All and Contains method like this, this will return true if both lists contains same elements even if the order is different:

bool control = list1.All(x => list2.Contains(x) && 
                          list1.Count(a => a == x) == list2.Count(b => b == x));

score:3

Try this:

bool equals = list1.OrderBy(x => x).SequenceEqual(list2.OrderBy(y => y));

I hope this helps ;)

score:0

You should consider using a HashSet, rather than a List. Its a little less expensive:

HashSet<string> firstSet = new HashSet<string>();
HashSet<string> secondSet = new HashSet<string>();

PopulateSets(firstSet, secondSet); //Example of method you might use to populate your hashsets.

bool isEqual = firstSet.SetEquals(secondSet);

From MSDN:

The SetEquals method ignores duplicate entries and the order of elements in the other parameter.


Related Articles