Can you run a Linq query on a jagged array?

P

pinkfloydfan

Hi there

I have a C# program that produces a lot of data that I currently store
in a jagged array and I have a couple of questions that I was hoping
someone might shed some light on for me please:

1) If the data is stored in MyData [250,000][240] is there a way to
do a query on the jagged array to get out say all the 100th entries of
the 250,000 main arrays or am I better off with a for loop?
2) Do you have a better suggestion of how to store the data rather
than a jagged array?

Thanks a lot
Lloyd
 
M

Marc Gravell

Can you? absolutely; the outer array is enumerable, as are the inner
arrays. But in this case, a loop is probably clearer.

As alternatives to a jagged array... what is the data, and what do you
need to do with it? In particular, the [250000, 240] doesn't look very
jagged - it looks rectangular. So maybe a rectangular array would suffice.

If you need to be able to add/remove rows, the a List<T[]> might do (I'd
probably try to avoid a List<List<T>> unless it is genuinely less
confusing in a given scenario).

Marc
 
P

pinkfloydfan

Hi Marc

I see your point about it not being very jagged. The program creates
a set of 240 doubles and repeats it 250,000 times. I stored them in a
jagged array in order to easily reference any single set but as it
happens I also need to go across sets at the same point.

I have all the data manipulation I need already written and working
fine its just that I have never used Linq queries so I thought that I
might teach myself by trying to re-write this code with Linq instead.

So, I have seen how to run a query on a single array, you say it can
be done on a jagged array (and I guess also on a multi-dimensional
array), so can you show me an example of how to do this please?

Thanks again
Lloyd
 
J

Jon Skeet [C# MVP]

I see your point about it not being very jagged.  The program creates
a set of 240 doubles and repeats it 250,000 times. I stored them in a
jagged array in order to easily reference any single set but as it
happens I also need to go across sets at the same point.

I have all the data manipulation I need already written and working
fine its just that I have never used Linq queries so I thought that I
might teach myself by trying to re-write this code with Linq instead.

So, I have seen how to run a query on a single array, you say it can
be done on a jagged array (and I guess also on a multi-dimensional
array), so can you show me an example of how to do this please?

A jagged array is just an array of arrays. So, to get an
IEnumerable<double> from a jagged array declared as:

double[][] samples = new double[250000][240];

where you want the 100th element of each top level array, you'd just
do:

samples.Select(innerArray => innerArray[100]);

Jon
 
M

Marc Gravell

you say it can
be done on a jagged array (and I guess also on a multi-dimensional
array), so can you show me an example of how to do this please?

Actually, multi-dimensional is tricker for LINQ, since T[,] doesn't have
an implicit conversion to IEnumerable<T>.

Anyway, some examples; however, I'll let you be the judge of whether
they add/detract in this case.

Marc
// create some jagged data
int[][] data = new int[3][];
for (int i = 0; i < 3; i++)
{
int width;
switch (i)
{ // true jagged
case 0: width = 5; break;
case 1: width = 3; break;
case 2: width = 7; break;
default: width = 1; break;
}
data = new int[width];
for (int j = 0; j < width; j++)
{
data[j] = 20 * i + j;
}
}
Console.WriteLine("Example 1");
// example 1: flatten the arrays into a single stream
var allData = data.SelectMany(row => row);
foreach (int cell in allData)
{
Console.WriteLine(cell);
}
Console.WriteLine("Example 2");
// example 2: selecting a column (confident it exists)
var column = from row in data
select row[2];
foreach (int cell in column)
{
Console.WriteLine(cell);
}

Console.WriteLine("Example 3");
// example 3: selecting a column (not sure it exists, quite
inefficient)
var sparseColumn = data.SelectMany(row => row.Skip(4).Take(1));
foreach (int cell in sparseColumn)
{
Console.WriteLine(cell);
}

Console.WriteLine("Example 4");
// example 4: selecting a column (not sure it exists, more
efficient)
sparseColumn = from row in data
where row.Length > 4
select row[4];
foreach (int cell in sparseColumn)
{
Console.WriteLine(cell);
}
 
P

pinkfloydfan

Dear Jon and Marc

Thanks very much for your examples, I will study them so I can see how
they work.

All the best
Lloyd
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top