If I have a list containing an arbitrary number of lists, like so:
var myList = new List<List<string>>()
{
new List<string>() { "a", "b", "c", "d" },
new List<string>() { "1", "2", "3", "4" },
new List<string>() { "w", "x", "y", "z" },
// ...etc...
};
...is there any way to somehow "zip" or "rotate" the lists into something like this?
{
{ "a", "1", "w", ... },
{ "b", "2", "x", ... },
{ "c", "3", "y", ... },
{ "d", "4", "z", ... }
}
The obvious solution would be to do something like this:
public static IEnumerable<IEnumerable<T>> Rotate<T>(this IEnumerable<IEnumerable<T>> list)
{
for (int i = 0; i < list.Min(x => x.Count()); i++)
{
yield return list.Select(x => x.ElementAt(i));
}
}
// snip
var newList = myList.Rotate();
...but I was wondering if there was a cleaner way of doing so, using linq or otherwise?