LINQ swap columns into rows

Viewed 17426

Is there a fancy LINQ expression that could allow me to do the following in a much more simpler fashion. I have a List<List<double>>, assuming the List are columns in a 2d matrix, I want to swap the list of columns into a list of rows. I have the following obvious solution:

int columns = 5;
var values; // assume initialised as List<List<double>>()

var listOfRows = new List<List<double>>();
for (int i = 0; i < columns ; i++)
{
    List<double> newRow = new List<double>();
    foreach (List<double> value in values)
    {
        newRow.Add(value[i]);
    }
    listOfRows.Add(newRow);
}
5 Answers
Related