Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Array get min value bigger than other value

Tags:

c#

linq

I have following array:

float[] arr = { 0, 0.1f, 0, 0.1f, 0, 0.2f };

What is the most elegant way to select min value that is bigger than 0 or bigger than some other value?

I've tried using Min() and Select...From...OrderBy...First() but no luck until now.

like image 870
VladL Avatar asked Jan 09 '13 15:01

VladL


2 Answers

Use the LINQ method Where to filter out zero values then use the LINQ method Min to retrieve the lowest value of the resulting collection.

arr.Where(f => f > 0).Min();
like image 133
Rotem Avatar answered Sep 20 '22 00:09

Rotem


You can exclude values using Where and then apply a Min:

array.Where(a => a > 1 && a < 10).Min();
like image 30
as-cii Avatar answered Sep 18 '22 00:09

as-cii