旋转-转置List< List< string>>.使用LINQ C#

旋转-转置List< List< string>>.使用LINQ C#

问题描述:

我有一个List<List<string>>,它是从远程数据源(即WCF)返回的.因此,我需要使用LINQ将以下数据修改为用户友好列表

I'm having a List<List<string>>, which is return from the remote data source (i.e., WCF). So, I need to modify the following data into a user-friendly list using LINQ

C#代码为

List<List<string>> PersonInfo = new List<List<string>>()
{
    new List<string>() {"John", "Peter", "Watson"},
    new List<string>() {"1000", "1001", "1002"}
}

适当的屏幕截图:现有

我需要像下面的屏幕截图一样旋转数据:建议

I need to rotate the data as like the below Screenshot: Proposed

请协助我使用 LINQ C#

这是一个简单而灵活的解决方案,它将处理多个内部列表,这些内部列表具有任意数量的维度.

This is a simple and flexible solution, it will handle multiple inner lists with any number of dimensions.

List<List<string>> PersonInfo = new List<List<string>>()
{
    new List<string>() {"John", "Peter", "Watson"},
    new List<string>() {"1000", "1001", "1002"}
};


var result = PersonInfo
    .SelectMany(inner => inner.Select((item, index) => new { item, index }))
    .GroupBy(i => i.index, i => i.item)
    .Select(g => g.ToList())
    .ToList();