How to create a lambda that will find closest number by order?

  • Thread starter Thread starter Frankdot
  • Start date Start date
F

Frankdot

Guest
This text is reedited.

I try to find in order the closest number from my variable.

For exemple:

variable is fib = 1.23

A list produce a series of numbers:

1.22 , 1.26, 1.33, 1.21 , 1.215, 1.225 etc

The lambda need to find the closest number from 1.23 (1.225) than keep on looking for the next closest number from the previous one. In this case 1.22 which is the closest number from 1.225 than the next one 1.23 which is the closest number from 1.225.

I came up with that lambda expression:

var minDistance = list.Min(no => Math.Abs(fibo- no));
var closest = list.First(no => Math.Abs(fibo- no) == minDistance);

but only find the closest from the variable and wont go further.

Thank you

Continue reading...
 
Back
Top