Each time you iterate over the result, you're going to call that Where
filter... which removes the items from hashset
as it goes.
So after it's iterated once, hashset
will no longer have any of those items, so there'll be nothing left to return.
Basically, what you're observing is that a Where
clause with a side-effect is a bad idea.
You might want to use a Join
to perform the intersection instead:
return items.Join(keys, firstMemberSelector, key => key, (value, key) => value);
It's not quite the same, in that it won't be a set operation... but you could potentially fix that.