C# Combine many IList<char> to make a new List

207 views Asked by At

I have some permutations of type IList each one having 6 elements e.g. 1, 1, 3, 2, 2, 2

So in my permutations collection I may have 2 lists looking like 1, 1, 3, 2, 2, 2 & the other as 1, 1, 2, 3, 2, 2

I need to combine them to get the resulting combination being 1, 1 , 2/3, 2/3, 2, 2.

But the combining needs to be applied to all permutations in the list. The bellow code suggested works, however the combining should be reducing the items in the collection, but after combining them Im left with the same amount of permutations combined?

foreach (IList<char> p in permutationCollection)
    {
        var result = p.Zip(permutationCollection.ElementAt(x + 1), (first, second) => { if (first != second) { return first + " / " + second; } else return second.ToString(); });
    }
1

There are 1 answers

1
marcinn On BEST ANSWER

Linq Zip will do the trick http://msdn.microsoft.com/en-us/library/vstudio/dd267698(v=vs.100).aspx

i.e.

int[] numbers = { 1, 1, 3, 2, 2, 2 };
int[] words = { 1, 1, 2, 3, 2, 2 };

var result = numbers.Zip(words, (first, second) => {if(first != second) {return first + " / " + second;} else return second.ToString(); });