PC Review


Reply
Thread Tools Rate Thread

Can you run a Linq query on a jagged array?

 
 
pinkfloydfan
Guest
Posts: n/a
 
      24th Jun 2008
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
 
Reply With Quote
 
 
 
 
Marc Gravell
Guest
Posts: n/a
 
      24th Jun 2008
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
 
Reply With Quote
 
pinkfloydfan
Guest
Posts: n/a
 
      24th Jun 2008
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
 
Reply With Quote
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      24th Jun 2008
On Jun 24, 11:25*am, pinkfloydfan <lloyd.greens...@googlemail.com>
wrote:
> 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
 
Reply With Quote
 
Marc Gravell
Guest
Posts: n/a
 
      24th Jun 2008
> 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[i] = new int[width];
for (int j = 0; j < width; j++)
{
data[i][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);
}
 
Reply With Quote
 
pinkfloydfan
Guest
Posts: n/a
 
      24th Jun 2008
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
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Index was outside the bounds of the array. With linq query... Seb Microsoft ASP .NET 0 16th May 2008 08:11 AM
convert a jagged array into a multidimensional array TS Microsoft C# .NET 5 23rd Jan 2007 12:49 AM
Re: Jagged Array Problem Mattias Sjögren Microsoft C# .NET 0 3rd Jan 2007 07:50 PM
jagged array wilfredo Microsoft VB .NET 1 30th Nov 2003 01:38 AM
Best way to Dim a jagged array. Lance Microsoft VB .NET 11 1st Oct 2003 12:35 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 10:03 PM.