views:

89

answers:

3

How would you obtain the min and max of a two-dimensional array using LINQ? And to be clear, I mean the min/max of the all items in the array (not the min/max of a particular dimension).

Or am I just going to have to loop through the old fashioned way?

A: 

You could implement a List> and find the min and max foreach and store it to a List and then you can easily find Min() and Max() from that list of all the values in a single dimensional List. That is the first thing that comes to mind, I am curious of this myself and am gonna see if google can grab a more clean cut approach.

Gnostus
How would you even get a two-dimensional array into a list in the first place?
DanM
Ah, I thought to far ahead, now I realize if you were to get the array into a list, you could use the code to get the MIN/MAX out of the same loop i envisioned to populate the Lists.:/ tis been a long mourning with not enough coffee :D
Gnostus
+7  A: 

Since Array implements IEnumerable you can just do this:

var arr = new int[2, 2] {{1,2}, {3, 4}};
int max = arr.Cast<int>().Max();    //or Min
Lee
Works like a charm, thanks! +1 and check mark!
DanM
+4  A: 

This seems to work:

IEnumerable<int> allValues = myArray.Cast<int>();
int min = allValues.Min();
int max = allValues.Max();
Mark Byers