I had recent need for a transpose operation which could swap the columns and rows of a nested IEnumerable sequence, it's simple enough to express in LINQ but after a quick search, all the solutions posted online are rather ugly. Here's a concise and elegant version expressed using LINQ query syntax:
This extension method will be available in the forthcoming Sasa 0.9.3 release.
/// <summary>It simply numbers the columns in each row, flattens the sequence of cells, and groups the entries by number. If the table has entries that are missing, this algorithm has the side-effect of compacting all entries so that only the last row or column will be missing the elements. This may or may not be suitable for your application.
/// Swaps the rows and columns of a nested sequence.
/// </summary>
/// <typeparam name="T">The type of elements in the sequence.</typeparam>
/// <param name="source">The source sequence.</param>
/// <returns>A sequence whose rows and columns are swapped.</returns>
public static IEnumerable<IEnumerable<T>> Transpose<T>(
this IEnumerable<IEnumerable<T>> source)
{
return from row in source
from col in row.Select(
(x, i) => new KeyValuePair<int, T>(i, x))
group col.Value by col.Key into c
select c as IEnumerable<T>;
}
This extension method will be available in the forthcoming Sasa 0.9.3 release.
Comments
please give correct method which converts rows into columns or vice versa.