I want to write a small piece of test code to remind me that certain collections are equivalent i.e. simple, self-contained, easy to read and LINQ Lambda oriented (to fulfill a personal learning target).
Here's what I have come up with:
var e = new IEnumerable<int> [] {
Enumerable.Range(100, 4).ToArray(),
new int[] { 100, 101, 102, 103 },
new [] { 100, 101, 102, 103 },
Enumerable.Range(100, 4).ToList(),
new List<int> { 100, 101, 102, 103 }
};
var permutations = e.SelectMany(s => e, ( lhs, rhs ) => new { lhs, rhs })
.Where( x => !x.lhs.Equals ( x.rhs ) );
foreach (var item in permutations)
{
Assert.That( item.lhs, Is.EqualTo( item.rhs ) );
}
Q1. Is there a 'simple' alteration to yield 10 combinations of pairs (I currently have 20 permutations of pairs)? By 'simple' I mean using existing LINQ operators, rather than, say writing a recursive extension method.
Q2. Is there a better way of asserting "all members of the array are equivalent" in context?
As regards Q1, this would seem to give me the right-hand side (rhs) but how to I 'carry-through' (or rejoin to) the 'original' to give me the left-hand side (lhs)?:
var r = e.SelectMany(( e1, i ) => e.Skip( i + 1 ));
(100,101,102,103)
contains same elements of e.g.(103,102,100,101)
? – Teejay Jan 9 '13 at 14:46