Was curiouis and looked around for a LINQ permutation solution and found one here:
What is the best way to find all combinations of items in an array?
Here is the code in question:
static IEnumerable<IEnumerable<T>> GetPermutations<T>(IEnumerable<T> list, int length)
{
if (length == 1) return list.Select(t => new T[] { t });
return GetPermutations(list, length - 1)
.SelectMany(t => list.Where(o => !t.Contains(o)),
(t1, t2) => t1.Concat(new T[] { t2 }));
}
I understand the solution for the most part, but am unsure what t1
and t2
refer to in the SelectMany. I know t1
is a Enumerable and t2
is a T. Any explanations would be great, thanks!
Edit*
Oops, I was looking at the wrong SelectMany docs. The correct one signature is here https://msdn.microsoft.com/en-us/library/bb534631(v=vs.110).aspx.