title: Iterating through a TimeSpan and running an action every month (or arbitrary unit of time)
body: <p>I'm writing a search program that includes a date range- DateFrom and DateTo, both of which are DateTimes. Searching January - April for any search criteria will return the results of January + February + March + April.</p>
  
  <p>I would like to add functionality whereby a user can choose to search each month within a - range, so searching January - April will return each individual month's results. However I'm having trouble finding an intelligent way to implement this for any unit of time larger than days.</p>
  
  <p>So far I'm getting a TimeSpan using:</p>
  
  <pre><code>TimeSpan ts = query.DateTo - query.DateFrom;
  </code></pre>
  
  <p>In a perfect world I'd just be able to do something like <code>foreach (month m in TimeSpan){dostuff}</code>, however TimeSpan stores dates as integers and does not include any units larger than days. Additionally, I thought maybe I could just use <code>n = DateFrom.month - DateTo.month</code> to get the difference in months and run a function in a for loop starting with DateFrom and lasting n months, but this won't work between years.</p>
  
  <p>The last case is definitely fixable but includes a number of tedious special cases. Is there a cleaner / more elegant way of accomplishing this sort of iteration that I'm missing?</p>
  
  <p>Thanks.</p>