title: In C#, is there a kind of a SortedList<double> that allows fast querying (with LINQ) for the nearest value?
body: <p>I am looking for a structure that holds a sorted set of double values. I want to query this set to find the closest value to a specified reference value.</p>
  
  <p>I have looked at the <code>SortedList&lt;double, double&gt;</code>, and it does quite well for me. However, since I do not need explicit key/value pairs. this seems to be overkill to me, and i wonder if i could do faster.</p>
  
  <p>Conditions: </p>
  
  <ul>
  <li>The structure is initialised only once, and does never change (no insert/deletes)</li>
  <li>The amount of values is in the range of 100k.</li>
  <li>The structure is queried often with new references, which must execute <strong>fast</strong>.</li>
  <li>For simplicity and speed, the set's value just below of the reference may be returned, not actually the nearest value</li>
  <li>I want to use LINQ for the query, if possible, for simplicity of code.</li>
  <li>I want to use no 3rd party code if possible. .NET 3.5 is available.</li>
  <li>Speed is more importand than memory footprint</li>
  </ul>
  
  <p>I currently use the following code, where <code>SortedValues</code> is the aforementioned <code>SortedList</code></p>
  
  <pre><code>    IEnumerable&lt;double&gt; nearest = from item in SortedValues.Keys
                                    where item &lt;= suggestion
                                    select item;
      return nearest.ElementAt(nearest.Count() - 1);
  </code></pre>
  
  <p><strong>Can I do faster?</strong></p>
  
  <p>Also I am not 100% percent sure, if this code is really safe. <code>IEnumerable</code>, the return type of my query is not by definition sorted anymore. However, a Unit test with a large test data base has shown that it is in practice, so this works for me. Have you hints regarding this aspect?</p>
  
  <p><em>P.S. I know that there are many similar questions, but none actually answers my specific needs. Especially there is this one <a href="https://stackoverflow.com/questions/1363773/c-data-structure-like-dictionary-but-without-a-value">C# Data Structure Like Dictionary But Without A Value</a>, but the questioner does just want to check the existence not find anything.</em></p>