使用 LINQ 在键索引上连接多维数组
我有 N 个多维源数据数组,每个数组具有相同的列数(本例中 C=4),但行数任意:
var array1 = new double[,]
{
{ 1, 2, 3, 4 },
{ 5, 6, 7, 8 },
{ 9, 10, 11, 12 }
};
var array2 = new double[,]
{
{ 1, 2, 5, 6 },
{ 7, 8, 9, 10 },
{ 9, 10, 11, 12 }
};
var array3 = new double[,]
{
{ 1, 2, 7, 8 },
{ 13, 14, 15, 16 }
};
...
var arrayN = new double[,] { ... };
我还有一个数组,指定源数组中的哪些索引将用作连接键:
var keyArray = new int[] { 0, 1 };
我需要以生成的数组如下所示的方式连接数组:
var result = new double[,]
{
// The length of each element in this array will be (C x N),
// the first C elements will be from array1, the next C from
// array2, and so on, and nulls used for arrays elements that
// are not included in the join (keys don't match).
//
// The number of rows in this array will be the number of distinct key combinations.
{ 1, 2, 3, 4, 1, 2, 5, 6, 1, 2, 7, 8 },
{ 5, 6, 7, 8, null, null, null, null, null, null, null, null },
{ 9, 10, 11, 12, 9, 10, 11, 12, null, null, null, null },
{ null, null, null, null, 7, 8, 9, 10, null, null, null, null },
{ null, null, null, null, null, null, null, null, 13, 14, 15, 16 }
};
我想我需要从每个源数组中选择不同的键并循环遍历所有数据并比较每一行等以填充结果数组。然而,应该有一种更有效的方法来使用 LINQ 来做到这一点 - 有人可以帮忙吗?
I have N multidimensional source data arrays, each with the same number of columns (C=4 in this example), but any number of rows:
var array1 = new double[,]
{
{ 1, 2, 3, 4 },
{ 5, 6, 7, 8 },
{ 9, 10, 11, 12 }
};
var array2 = new double[,]
{
{ 1, 2, 5, 6 },
{ 7, 8, 9, 10 },
{ 9, 10, 11, 12 }
};
var array3 = new double[,]
{
{ 1, 2, 7, 8 },
{ 13, 14, 15, 16 }
};
...
var arrayN = new double[,] { ... };
I also have an array that specifies which indices in the source arrays are to be used as the join keys:
var keyArray = new int[] { 0, 1 };
I need to join the arrays in such as way that the resulting array will look like:
var result = new double[,]
{
// The length of each element in this array will be (C x N),
// the first C elements will be from array1, the next C from
// array2, and so on, and nulls used for arrays elements that
// are not included in the join (keys don't match).
//
// The number of rows in this array will be the number of distinct key combinations.
{ 1, 2, 3, 4, 1, 2, 5, 6, 1, 2, 7, 8 },
{ 5, 6, 7, 8, null, null, null, null, null, null, null, null },
{ 9, 10, 11, 12, 9, 10, 11, 12, null, null, null, null },
{ null, null, null, null, 7, 8, 9, 10, null, null, null, null },
{ null, null, null, null, null, null, null, null, 13, 14, 15, 16 }
};
I am thinking I need to select the distinct keys from each source array and loop through all of the data and compare each row, etc. to fill the results array. However, there should be a more efficient way to do this using LINQ - can anyone help?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您使用 double[][] 而不是 double[,],这是一个解决方案
Here is a solution if you use double[][] instead of double[,]
我有一个解决方案给你。它可能不像您想要的那么干净,但它会起作用。这需要您更改数组的使用:
var array1 = new double[,]
到:
var array1 = new double?[][]
as .NET 将第一个视为单个 IEnumerable 而不是 IEnumerable>。另外,为了支持 null,您必须使用 nullable-double。以下代码确实期望所有锯齿状数组具有相同的大小。
接下来,您必须定义一个类来保存动态键并进行比较:
然后您可以使用以下逻辑来执行查询并返回您期望的双精度?[][]:
I have a solution for you. It may not be as clean as you are looking for, but it will work. It would require that you change your use of arrays from:
var array1 = new double[,]
to:
var array1 = new double?[][]
as .NET views the first as a single IEnumerable instead of IEnumerable>. Plus to support the nulls you have to use a nullable-double. The following code does expect that all jagged arrays are of the same size.
Next you have to define a class to hold the dynamic key(s) and do the compare:
Then you have the following logic to do the query and return an double?[][] that you would expect: