title: LINQ Query - nearest adjacent date
body: <p>I am trying to write a method that will determine the closest date chronlogically, given a list of dates and a target date. For example, given a (simplified) date set of {Jan 2011, March 2011, November 2011}, and a target date of April 2011, the method would return March 2011</p>
  
  <p>At first, I was thinking of using LINQ's skip, but I'm not sure of an appropriate Func such that it would stop <strong>before</strong> the date was exceeded. This below seems a viable solution, but I'm not sure if there's a more efficient means of doing this. Presumably Last and First would be linear time each.</p>
  
  <p>The source dataSet can be between 0 and 10,000 dates, generally around 5,000. Also, I am iterating over this whole process between 5 and 50 times (this is the number of target dates for different iterations). </p>
  
  <pre><code>// assume dateSet are ordered ascending in time.
  public DateTime GetClosestDate(IEnumerable&lt;DateTime&gt; dateSet, DateTime targetDate)
  {
     var earlierDate = dateSet.Last(x =&gt; x &lt;= targetDate);
     var laterDate = dateSet.First(x =&gt; x &gt;= targetDate);
  
     // compare TimeSpans from earlier to target and later to target.
     // return closestTime;
  }
  </code></pre>