How to find the nearest and the larger from a list of dates?
<p>Given a start date, how do I find the larger and nearest start date from a list?</p>

<p>Here's my code but my way of getting the nearest one seems to be incorrect:</p>

<pre><code>Activity oActivityNearestLarger = null;

List&lt;Activity&gt; oActivityByStartDateList = ClassForDBCalls.ActivityLISTByOutingID(iOutingID)
    .OrderBy(a =&gt; a.StartDate)
    .ToList();

long lLeastTicksDifference = 0; // initialize
int iIndexOfTheNearestLargerActivity = 0;

for (int i = 0; i &lt; oActivityByStartDateList.Count; i++)
{
    // the start of the loop, set the lRecordTicksDifference
    if (i == 0)
    {
        lLeastTicksDifference = Math.Abs(oActivityByStartDateList[0].StartDate.Value.Subtract(dtStartDate).Ticks);
    }

    long lCurrentTicksDifference = Math.Abs(oActivityByStartDateList[i].StartDate.Value.Subtract(dtStartDate).Ticks);

    // get the larger, closest start datetime in the activities for the outing
    if (oActivityByStartDateList[i].StartDate &gt; dtStartDate)
    {
        //  is the current activity startdate closer 
        //  to the to be added startdate (is it the nearest to the datetime being added?)
        if (lCurrentTicksDifference &lt;= lLeastTicksDifference)
        {
            // if it has the least difference (meaning closer) store it for comparison in the next iteration
            lLeastTicksDifference = lCurrentTicksDifference;

            oActivityNearestLarger = oActivityByStartDateList[i];

            // set the index to be used to determine the Activity previous to it
            iIndexOfTheNearestLargerActivity = i;
        }
    }
}
</code></pre>

<p>Thanks in advance!</p>
