title: Find the List index of the object containing the closest property value
body: <p>How can I find the List index of the object containing the closest property value?</p>
  
  <p>Sample, class MyData contains a property Position. class MyDataHandler has a List of MyData and the positions are: 1, 3, 14, 15, 22.</p>
  
  <p>MyDataHandler has a method called GetClosestIndexAt, If the input value is 13, the method must return index 2.</p>
  
  <p>Sample code:</p>
  
  <pre><code>public class MyData
  {
      public double Position { get; set; }
      public string Name { get; set; }
  }
  
  public class MyDataHandler
  {
      private List&lt;MyData&gt; myDataList = new List&lt;MyData&gt;();
  
      public MyDataHandler()
      {
          FillMyData(myDataList);
      }
  
      public int GetClosestIndexAt(double position)
      {
          int index = -1;
          //How to get the index of the closest MyDataList.Position to position value.
          //index = ?????
          return index;
      }
  
      private void FillMyData(List&lt;MyData&gt; MyDataList)
      {
          //fill the data...
      }
  }
  </code></pre>