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

c# - how to write client side events functions for the combobox items -

exception - Python, pyPdf OCR error: pyPdf.utils.PdfReadError: EOF marker not found -