I'm trying to round a result value to the next number in a list.
If the value is (187) I need to set the result to (240)
int[] list = new int[] { 16, 25, 35, 50, 70, 95, 120, 150, 185, 240, 300, 400 };
double max;
max = list.Where(x => x <= result).Max();
But this does not work.
You're close:
list.Where(x => x >= result).Min();
You just want the first item greater than or equal to your expected result:
var max = list.FirstOrDefault(x => x >= result)
NOTE: This assumes the list is ordered, as your example seems to suggest.
If you want to get an exception if there's no match then just use First:
var max = list.First(x => x >= result)
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