Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Max value from list that is smaller than X

Tags:

c#

linq

How to retrieve int value from List that is biggest number on it but still smaller than X value to which i am comparing.

Value = 10

Example one: List = {1,4,6,8};  => number 8 biggest on list and smaller than 10
Example two: List = {1,15,17,20}; => number 1 biggest on list and smaller than 10

I was trying using Linq but no success so far.

like image 597
Tagyoureit Avatar asked Nov 30 '22 00:11

Tagyoureit


1 Answers

You can just restrict the values you use to get the "Max" by using a Where clause:

return myList.Where(x => x < 10).Max();
like image 130
Grant Winney Avatar answered Dec 16 '22 10:12

Grant Winney