LINQ查询toarray() - 如何访问数组? [英] LINQ query toarray() - how to access array?
问题描述
我有一个LINQ查询,它给我一个与索引相关的平均值。我已经将查询转换为数组,但我无法弄清楚如何对数组做任何事情(我需要使用查询数据)。我的LINQ只是如此如此
i have a LINQ query that gives me an average linked to an index. I've turned the query into an array, but i can't figure out how do anything with the array (i need to use the query data). My LINQ is only so so
var vavg = (from z in data
where z.Y > 0 && z.Y < 0.1
group z by z.X into XGroup
select new
{
X = XGroup.Key,
vavg = XGroup.Average(z => z.A),
}).ToArray();
i得到正确的结果,但查询不是以真正的数组返回,我需要对它们进行操作,所以我需要一个double [] [],其中[0]是X而[1]是vavg
我尝试过:
vavg.Average();返回错误
double [] [] test = new double [2] [];
test [0] = vavg [0];返回错误
错误似乎与数组的结构有关,这不是一个真正的数组,看起来更像是一个数据结构给我
i get the correct results but the query isn't returned in a true array, i need to do operations on them so i need a double[][] where [0] is X and [1] is vavg
What I have tried:
vavg.Average(); returns error
double[][] test = new double[2][];
test[0] = vavg[0]; returns error
the errors seem to be related to the structure of the Array which isn't a "real" array and looks more like a data structure to me
推荐答案
你需要更改你的选择,以便它创建你的锯齿状数组元素。
you need to change your select so that it creates your jagged array elements.
var vavg = (from z in data
where z.Y > 0 && z.Y < 0.1
group z by z.X into XGroup
select new double []
{
XGroup.Key,
XGroup.Average(z => z.A),
}).ToArray();
参见 new double []
在上选择
。还摆脱了那里的临时任务 - 他们不需要。
See the declaration of the new double[]
on the select
. Also got rid of the temporary assignments in there - they're not needed.
这篇关于LINQ查询toarray() - 如何访问数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!