This may be very similar to this question , but I would like to know the most efficient way in C # and LINQ to compare a list of elements with each other in the same list.
For example, in pseudo code, I would like to do this:
foreach(i in list)
foreach(j in list.Except(i))
Compare(j,i)
I know that Except accepts an enumeration instead of a single element and may not be a better idea, but this should illustrate my intentions.
Any ideas?
Update:
I think this question was a bit vague. The goal was to iterate over the list twice (using LINQ), skipping a pair (i, i); no matter what Compare(i,j)really doesn't matter to my question.
Then there are two cases where (i,j) == (j,i)and (i,j) != (j,i). For the former, George Duckett's decision below does the trick, but what about the latter? It was here that my initial use of Except was such that both (i,j), and so were evaluated (j,i).
So, to clarify, is there a better way to skip an item in the list besides list.Except(Enumerable.Repeat(i,1))?
source
share