LINQ将列交换为行 [英] LINQ swap columns into rows
本文介绍了LINQ将列交换为行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否有一个奇特的LINQ表达式,可以使我以更简单的方式执行以下操作.我有一个List<List<double>>
,假设列表是二维矩阵中的列,我想将列列表交换为行列表.我有以下明显的解决方案:
Is there a fancy LINQ expression that could allow me to do the following in a much more simpler fashion. I have a List<List<double>>
, assuming the List are columns in a 2d matrix, I want to swap the list of columns into a list of rows. I have the following obvious solution:
int columns = 5;
var values; // assume initialised as List<List<double>>()
var listOfRows = new List<List<double>>();
for (int i = 0; i < columns ; i++)
{
List<double> newRow = new List<double>();
foreach (List<double> value in values)
{
newRow.Add(value[i]);
}
listOfRows.Add(newRow);
}
推荐答案
您可以非常轻松地LINQify内部循环:
You could LINQify the inner loop pretty easily:
vector.AddRange(values.Select(value => value[i]));
是否提高可读性完全取决于您!
Whether or not that improves the readability is left entirely up to you!
这篇关于LINQ将列交换为行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文