myList[0] = { "1", "5", "3", "9" }; myList[1] = { "2", "3" }; myList[2] = { "93" }; 1 2 93 1 3 93 5 2 93 5 3 93 3 2 93 3 3 93 9 2 93 9 3 93 from val0 in new []{ "1", "5", "3", "9" } from val1 in new []{ "2", "3" } from val2 in new []{ "93" } select String.Format("val0={0};val1={1};val2={2}", val0, val1, val2) static List foo(int a, List x) { List retval= new List(); if (a == x.Count) { retval.Add(""); return retval; } foreach (Object y in x[a]) { foreach (string x2 in foo(a + 1, x)) { retval.Add(y.ToString() + " " + x2.ToString()); } } return retval; } static void Main(string[] args) { List myList = new List(); myList.Add(new string[0]); myList.Add(new string[0]); myList.Add(new string[0]); myList[0] = new string[]{ "1", "5", "3", "9" }; myList[1] = new string[] { "2", "3" }; myList[2] = new string[] { "93" }; foreach (string x in foo(0, myList)) { Console.WriteLine(x); } Console.ReadKey(); } foreach (String s1 in array1) { foreach (String s2 in array2) { foreach (String s3 in array3) { String result = s1 + " " + s2 + " " + s3; //do something with the result } } } private ArrayList permute(ArrayList> ar, int startIndex) { if (ar.Count == 1) { foreach(String s in ar.Value(0)) { ar.Value(0) = "val" + startIndex + "=" + ar.Value(0); return ar.Value(0); } ArrayList ret = new ArrayList(); ArrayList tmp1 ar.Value(0); ar.remove(0); ArrayList tmp2 = permute(ar, startIndex+1); foreach (String s in tmp1) { foreach (String s2 in tmp2) { ret.Add("val" + startIndex + "=" + s + " " + s2); } } return ret; } static void Main(string[] args) { string[][] myList = new string[3][]; myList[0] = new string[] { "1", "5", "3", "9" }; myList[1] = new string[] { "2", "3" }; myList[2] = new string[] { "93" }; List permutations = new List(myList[0]); for (int i = 1; i < myList.Length; ++i) { permutations = RecursiveAppend(permutations, myList[i]); } //at this point the permutations variable contains all permutations } static List RecursiveAppend(List priorPermutations, string[] additions) { List newPermutationsResult = new List(); foreach (string priorPermutation in priorPermutations) { foreach (string addition in additions) { newPermutationsResult.Add(priorPermutation + ":" + addition); } } return newPermutationsResult; } static void Main(string[] args) { string[][] myList = new string[3][]; myList[0] = new string[] { "1", "5", "3", "9" }; myList[1] = new string[] { "2", "3" }; myList[2] = new string[] { "93" }; List> permutations = new List>(); foreach (string init in myList[0]) { List temp = new List(); temp.Add(init); permutations.Add(temp); } for (int i = 1; i < myList.Length; ++i) { permutations = RecursiveAppend(permutations, myList[i]); } //at this point the permutations variable contains all permutations foreach (List list in permutations) { foreach (string item in list) { Console.Write(item + ":"); } Console.WriteLine(); } } static List> RecursiveAppend(List> priorPermutations, string[] additions) { List> newPermutationsResult = new List>(); foreach (List priorPermutation in priorPermutations) { foreach (string addition in additions) { List priorWithAddition = new List(priorPermutation); priorWithAddition.Add(addition); newPermutationsResult.Add(priorWithAddition); } } return newPermutationsResult; } Public Shared Function GetCombinationsFromIEnumerables(ByRef chain() As Object, ByRef IEnumerables As IEnumerable(Of IEnumerable(Of Object))) As List(Of Object()) Dim Combinations As New List(Of Object()) If IEnumerables.Any Then For Each v In IEnumerables.First Combinations.AddRange(GetCombinationsFromIEnumerables(chain.Concat(New Object() {v}).ToArray, IEnumerables.Skip(1)).ToArray) Next Else Combinations.Add(chain) End If Return Combinations End Function Public Shared Function GetCombinationsFromIEnumerables(ByVal ParamArray IEnumerables() As IEnumerable(Of Object)) As List(Of Object()) Return GetCombinationsFromIEnumerables(chain:=New Object() {}, IEnumerables:=IEnumerables.AsEnumerable) End Function public static List GetCombinationsFromIEnumerables(ref object[] chain, ref IEnumerable> IEnumerables) { List Combinations = new List(); if (IEnumerables.Any) { foreach ( v in IEnumerables.First) { Combinations.AddRange(GetCombinationsFromIEnumerables(chain.Concat(new object[] { v }).ToArray, IEnumerables.Skip(1)).ToArray); } } else { Combinations.Add(chain); } return Combinations; } public static List GetCombinationsFromIEnumerables(params IEnumerable[] IEnumerables) { return GetCombinationsFromIEnumerables(chain = new object[], IEnumerables = IEnumerables.AsEnumerable); } Dim list1 = New String() {"hello", "bonjour", "hallo", "hola"} Dim list2 = New String() {"Erwin", "Larry", "Bill"} Dim list3 = New String() {"!", ".."} Dim result = MyLib.GetCombinationsFromIEnumerables(list1, list2, list3) For Each r In result Debug.Print(String.Join(" "c, r)) Next object list1 = new string[] {"hello","bonjour","hallo","hola"}; object list2 = new string[] {"Erwin", "Larry", "Bill"}; object list3 = new string[] {"!",".."}; object result = MyLib.GetCombinationsFromIEnumerables(list1, list2, list3); foreach (r in result) { Debug.Print(string.Join(' ', r)); } public static IEnumerable> GetPermutations(IEnumerable> lists) { // Check against an empty list. if (!lists.Any()) { yield break; } // Create a list of iterators into each of the sub-lists. List> iterators = new List>(); foreach (var list in lists) { var it = list.GetEnumerator(); // Ensure empty sub-lists are excluded. if (!it.MoveNext()) { continue; } iterators.Add(it); } bool done = false; while (!done) { // Return the current state of all the iterator, this permutation. yield return from it in iterators select it.Current; // Move to the next permutation. bool recurse = false; var mainIt = iterators.GetEnumerator(); mainIt.MoveNext(); // Move to the first, succeeds; the main list is not empty. do { recurse = false; var subIt = mainIt.Current; if (!subIt.MoveNext()) { subIt.Reset(); // Note the sub-list must be a reset-able IEnumerable! subIt.MoveNext(); // Move to the first, succeeds; each sub-list is not empty. if (!mainIt.MoveNext()) { done = true; } else { recurse = true; } } } while (recurse); } } public static IEnumerable> GetPermutations(IEnumerable collection) where T : IComparable { if (!collection.Any()) { return new List>() {Enumerable.Empty() }; } var sequence = collection.OrderBy(s => s).ToArray(); return sequence.SelectMany(s => GetPermutations(sequence.Where(s2 => !s2.Equals(s))).Select(sq => (new T[] {s}).Concat(sq))); } class Program { static void Main(string[] args) { var listofInts = new List>(3); listofInts.Add(new List{1, 2, 3}); listofInts.Add(new List { 4,5,6 }); listofInts.Add(new List { 7,8,9,10 }); var temp = CrossJoinLists(listofInts); foreach (var l in temp) { foreach (var i in l) Console.Write(i + ","); Console.WriteLine(); } } private static IEnumerable> CrossJoinLists(IEnumerable> listofObjects) { var result = from obj in listofObjects.First() select new List {obj}; for (var i = 1; i < listofObjects.Count(); i++) { var iLocal = i; result = from obj in result from obj2 in listofObjects.ElementAt(iLocal) select new List(obj){ obj2 }; } return result; } }