Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can use the List type to hold the pairs, Sort them and use List.BinarySearch.</p> <p>For instance you could have something like the following:</p> <pre><code>struct Pair { public Pair(DateTime t, int v) { date = t; value = v; } public DateTime date; public int value; } .... List&lt;Pair&gt; pairs = new List&lt;Pair&gt;(); pairs.Add(new Pair(DateTime.Now, 100)); pairs.Add(new Pair(DateTime.Now, 200)); .... // Sort using the time. pairs.Sort(delegate(Pair pair1, Pair pair2) { return pair1.date.CompareTo( pair2.date); } ); // Do binary search. int index = pairs.BinarySearch(new Pair(dateToSearch, 0), delegate(Pair pair1, Pair pair2) { return pair1.date.CompareTo(pair2.date); }); if (index &gt;= 0) { // Found the element! return pairs[index].value; } // If not found, List.BinarySearch returns the complement // of the index where the element should have been. index = ~index; // This date search for is larger than any if (index == pairs.Count) { // } // The date searched is smaller than any in the list. if (index == 0) { } // Otherwise return average of elements at index and index-1. return (pairs[index-1].value + pairs[index].value)/2; </code></pre> <p>Of course the code is not the best possible, but you get the idea: use List, Sort it and then do BinarySearch.</p> <p>Lookup MSDN for more information.</p> <p>List: <a href="http://msdn.microsoft.com/en-us/library/6sh2ey19.aspx" rel="noreferrer">http://msdn.microsoft.com/en-us/library/6sh2ey19.aspx</a></p> <p>List.Sort: <a href="http://msdn.microsoft.com/en-us/library/3da4abas.aspx" rel="noreferrer">http://msdn.microsoft.com/en-us/library/3da4abas.aspx</a> </p> <p>List.BinarySearch: <a href="http://msdn.microsoft.com/en-us/library/3f90y839.aspx" rel="noreferrer">http://msdn.microsoft.com/en-us/library/3f90y839.aspx</a></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload