Process every pair in a sequence
- by Henry Jackson
I'm looking for a concise way to process every (unordered) pair of elements in a sequence in .NET.
I know I can do it with nested foreach loops, but I was looking for something a little more readable.
I was imagining something like a modified Any() extension method:
IEnumerable<Transaction> transactions = ...
if (transactions.AnyPair( (first, second) => first.UniqueID == second.UniqueID))
throw ...
Or maybe a foreach-style one:
IEnumerable<JigsawPiece> pieces = ...
pieces.ForEachPair( (first, second) => {
TryFit(first, second);
});
This question has been asked for other languages (e.g. see http://stackoverflow.com/questions/942543/operation-on-every-pair-of-element-in-a-list), but I'm looking for a .NET solution.