Possible Duplicate:
Comparing two collections for equality
I have two lists
List<int> Foo = new List<int>(){ 1, 2, 3 };
and
List<int> Bar = new List<int>(){ 2, 1 };
To find out if they have same elements or not I did
if(Foo.Except(Bar).Any() || Bar.Except(Foo).Any())
{
//Do Something
}
but this requires two bool evaluations. First it does Foo.Except(Bar).Any() and then Bar.Except(Foo).Any(). Is there a way to do this in single evaluation?
You don’t have to check it twice.
Just do something like this (pay attention to Foo, it can be null and throw the related exception)
You might also want to check first you have to check if one of those lists or both are empty or null.. but only if that situation has any particular value for you.