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?
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
This seems to work:
IEnumerable<int> allValues = myArray.Cast<int>();
int min = allValues.Min();
int max = allValues.Max();
here is variant:
var maxarr = (from int v in aarray select v).Max();
where aarray is int[,]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With