score:5

Accepted answer

Any() doesn't do what you think it does. Without a lambda expression in Any(), it will just check if there is any element in the enumerable it is called on.

You either want:

types.Select((x, i) => values[i].GetType() != x).Any(x => x)

or maybe

types.Where((x, i) => values[i].GetType() != x).Any()

score:7

Any doesn't check what's in your sequence. It looks for if there is Any element in the sequence.So whether you have a sequence of true values of false doesn't matter.

score:8

Enumerable.Any without argument just checks if the sequence contains elements. If you want to know if there are any trues you have to use the overload:

bool anyTrue = bools.Any(b => b);

score:3

You need to use this overload of the Any() method. The overload you are using just returns true if the sequence contains stuff, it doesn't care what the values are. To check the booleans and only return if one them is true, your code would need to be

if (types.Select((x, i) => values[i].GetType() != x).Any(x => x))
    throw new Exception();

Related Articles