0

I should state that this is not a permutation issue and that I would like to do this in C#. I have a variable number of lists of strings and each list has a variable number of elements.

So for example:

List 1: One Two

List 2: Uno Dos Tres

List 3: 1 2 3 4

The results should end up being: OneUno1 OneUno2 OneUno3 OneUno4 OneDos1 OneDos2 OneDos3 OneDos4 OneTres1 OneTres2 OneTres3 OneTres4 TwoUno1 TwoUno2 TwoUno3 TwoUno4 TwoDos1 TwoDos2 TwoDos3 TwoDos4 TwoTres1 TwoTres2 TwoTres3 TwoTres4

The real issue here is that the final list can get very large very fast so I run into problems if I do this in memory, so my thought is to write the strings out to a file as I generate them but I'm having issues trying to figure out the logic/algorithm for doing this.

2 Answers2

2

Example:

    var list1 = new[]{"One", "Two"};
    var list2 = new[]{"Uno", "Dos", "Tres"};
    var list3 = new[]{1,2,3,4};

    var result = from a in list1
         from b in list2
         from c in list3
         select new {a, b, c};

    foreach(var item in result)
    {
        Console.WriteLine(item.a + " " + item.b + " " + item.c); 
    }
Backs
  • 24,430
  • 5
  • 58
  • 85
1

What you need is simple cross Join.

IEnumerable<string> results = from l1 in list1
    from l2 in list2
    from l3 in list3
    select string.Format("{0}{1}{2}", l1, l2, l3);

I'm not quite sure how large output in your case, if it is really large and want to write it to file in chunks you could do this.

Note previous Linq statement returns only IEnumerable<string> .

foreach(var smallchunk in results.Take(1000))
{
     System.IO.File.AppendAllLines("filepath", smallChunk); 
}
Hari Prasad
  • 16,716
  • 4
  • 21
  • 35