c# - How to get the list index of the nearest number? -


how list index can find closest number?

list<int> list = new list<int> { 2, 5, 7, 10 }; int number = 9;  int closest = list.aggregate((x,y) =>  math.abs(x-number) < math.abs(y-number) ? x : y); 

if want index of closest number trick:

int index = list.indexof(closest); 

Comments

Popular posts from this blog

Cursor error with postgresql, pgpool and php -

delphi - ESC/P programming! -

c++ - error: use of deleted function -