You can use something like this:
private static string[] TimeBucket = { "0D", "1D", "1W", "2W", "1M", "2M", "3M", "6M", "9M", "1Y", "2Y", "3Y", "5Y", "10Y", "15Y", "20Y", "25Y", ">=30Y" };
private static int[] TimeIntervals = { 0, 1, 7, 2 * 7, 30, 2 * 30, 3 * 30, 6 * 30, 9 * 30, 365, 2 * 365, 3 * 365, 5 * 365, 3650, 15 * 365, 20 * 365, 25 * 365, 30 * 365 };
private static string ComputeMaturity(DateTime? mat, DateTime? refDate)
{
if (!mat.HasValue)
return "Void";
int nbd = (mat - refDate).Value.Days;
for (int pos = 0; pos < TimeBucket.Length; pos++)
if (nbd < TimeIntervals[pos])
return TimeBucket[pos];
return TimeBucket[(TimeBucket).Length - 1];
}
During groupby,
var week = dt.AsEnumerable().GroupBy(row => ComputeMaturity(Convert.ToDateTime(row["Date"]),DateTime.Now))
.Select(g => new
{
Date = (g.Key),
}).ToList();
dataGridView1.DataSource = week;