I'm trying to permute multiple arrays of different sizes with null values. Something like:
IEnumerable<IEnumerable<T>> GetAllPermutations<T>(IEnumerable<IEnumerable<T>> lists)
{
// if it received a list with 4 elements, it must return a list of list with 4 permutations
}
Example:
var list1 = new List<string>() { "1", "2", "3", "4" };
var list2 = new List<string>() { "5", "6", "7", "8" };
var list3 = new List<string>() { "9", "7", null, "0" };
var fullList = new List<List<string>>() { list1, list2, list3 };
var result = GetAllPermutations(fullList);
Result: 81 permutations (3^4)
{ "1", "2", "3", "4" }
{ "5", "2", "3", "4" }
{ "1", "6", "3", "4" }
{ "1", "2", "7", "4" }
{ "1", "2", "3", "8" }
.
.
.
.
.
{ "1", "9", "null", "0" }
{ "9", "7", null, "0" }
I've tried a lot of stuff but none of it worked out. The best thing i've found was this answer from another question:
var cc = Enumerable
.Range(1, (1 << simpleList.Count) - 1)
.Select(index => simpleList.Where((item, idx) => ((1 << idx) & index) != 0).ToList());
But it doesn't work with multiple lists.
EDIT: The example above is a combinatoric answer which worked for me when i was working with another problem. It does generate some permutations but not all and it does not work with multiple lists.