Process every pair in a sequence

Posted by Henry Jackson on Stack Overflow See other posts from Stack Overflow or by Henry Jackson
Published on 2010-05-13T16:45:22Z Indexed on 2010/05/13 17:04 UTC
Read the original article Hit count: 242

Filed under:
|

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.

© Stack Overflow or respective owner

Related posts about .NET

Related posts about extension-methods