TransposeExtensionTransposeT Method

Transposes a sequence of rows into a sequence of columns.

Definition

Namespace: MoreLinq.Extensions
Assembly: MoreLinq (in MoreLinq.dll) Version: 3.4.0+b99a6a8cc504caf2d48372fe54a2f8116c59cd0c
C#
public static IEnumerable<IEnumerable<T>> Transpose<T>(
	this IEnumerable<IEnumerable<T>> source
)

Parameters

source  IEnumerableIEnumerableT
Source sequence to transpose.

Type Parameters

T
Type of source sequence elements.

Return Value

IEnumerableIEnumerableT
Returns a sequence of columns in the source swapped into rows.

Usage Note

In Visual Basic and C#, you can call this method as an instance method on any object of type IEnumerableIEnumerableT. When you use instance method syntax to call this method, omit the first parameter. For more information, see Extension Methods (Visual Basic) or Extension Methods (C# Programming Guide).

Remarks

If a rows is shorter than a follow it then the shorter row's elements are skipped in the corresponding column sequences. This operator uses deferred execution and streams its results. Source sequence is consumed greedily when an iteration begins. The inner sequences representing rows are consumed lazily and resulting sequences of columns are streamed.

Example

C#
var matrix = new[]
{
    new[] { 10, 11 },
    new[] { 20 },
    new[] { 30, 31, 32 }
};
var result = matrix.Transpose();
The result variable will contain [[10, 20, 30], [11, 31], [32]].

See Also