There is nothing to fix here. Any()
will iterate the enumeration but stop after the first element (after which it returns true).
Multiple enumerations are mainly a problem in two cases:
Performance: Generally you want to avoid multiple iterations if you
can, because it is slower. This does not apply here since Any()
will
just confirm there is at least one element and is a required check for you. Also you are not accessing any remote/external resources, just an in-memory sequence.
Enumerations that cannot be iterated over more than once: E.g.
receiving items from a network etc. - also does not apply here.
As a non Linq version that only needs to iterate once you could do the following:
bool foundAny= false;
bool isEqual = true;
if(f == null)
throw new ArgumentException();
foreach(var check in f)
{
foundAny = true;
isEqual = isEqual && check(p,p2);
}
if(!foundAny)
throw new ArgumentException();
return isEqual;
But, as noted, in your case it does not make a difference, and I would go with the version that is more readable to you.