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

查看:54
本文介绍了旋转 - 转置 List<List<string>>使用 LINQ C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 List>,它是从远程数据源(即 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<List<string>>使用 LINQ C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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