旋转-转置List< List< string>>.使用LINQ C# [英] Rotate - Transposing a List<List<string>> using LINQ C#

查看:116
本文介绍了旋转-转置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();

这篇关于旋转-转置List&lt; List&lt; string&gt;&gt;.使用LINQ C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆