Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
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

C#
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 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
Posted
Updated 5-Nov-16 17:51pm
Comments
[no name] 5-Nov-16 23:45pm    
ToArray does not return a multi-dimensional array.

1 solution

you need to change your select so that it creates your jagged array elements.
C#
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();

See the declaration of the new double[] on the select. Also got rid of the temporary assignments in there - they're not needed.
 
Share this answer
 
v2
Comments
Member 12815488 6-Nov-16 0:49am    
thanks! funny how simple some things end up. i literally couldn't find that answer on the web in an hour of googling. appreciate it.
Midi_Mick 6-Nov-16 1:13am    
No worries - it was a fun one, and had me scratching up a bald patch for a little while. But once I hit upon that what you needed was an array of arrays, the answer became obvious.
Wendelius 6-Nov-16 2:42am    
Nice example, a 5.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900