fork(2) download
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. public class Test
  6. {
  7. public static void Main()
  8. {
  9. List<double> myDataList = new List<double> {1, 3, 14, 15, 22};
  10. double position = 2.2;
  11. var res = myDataList
  12. .Select((v, i) => new {Position = v, Index = i}) // Pair up the position and the index
  13. .OrderBy(p => Math.Abs(p.Position - position)) // Order by the distance
  14. .First().Index;
  15. Console.WriteLine(res);
  16. }
  17. }
Success #stdin #stdout 0.06s 34080KB
stdin
Standard input is empty
stdout
1