如何在C#中将2D数组转换为2D列表
问题描述:
我有一个2D字符串数组.我想将其转换为
I have a 2D string array. I want to convert this into
List<List<string>>
如何在C#中实现这一目标?
How do I achieve this in C#?
答
使用Linq
,您可以执行此操作.
Using Linq
you could do this.
var result list.Cast<string>()
.Select((x,i)=> new {x, index = i/list.GetLength(1)}) // Use overloaded 'Select' and calculate row index.
.GroupBy(x=>x.index) // Group on Row index
.Select(x=>x.Select(s=>s.x).ToList()) // Create List for each group.
.ToList();
选中此 example