Monday, January 31, 2011

More fun with Linq - DateTime Quarters

Here's a fun little query to get the quarters for a calendar year... Put this straight into LinqPad and check it out. Of course, LinqPad still requires EOL underscores for line continuations hence the syntactic clutter.

Dim quarters = _
   From q In Enumerable.Range(1, 4).Select(Function(x) New DateTime(DateTime.Now.Year, ((x-1) * 3) + 1, 1)) _
   Select New With { _
      .QtrStart = q, _
      .QtrEnd = q.AddMonths(3).AddMilliseconds(-1) _
   }
quarters.Dump()