score:18

Accepted answer

If your column type is DateTime then you can group by its Hour property and get count for each hour like:

var query = dt.AsEnumerable()
    .GroupBy(row => row.Field<DateTime>("dateAndTime").Hour)
    .Select(grp => new
    {
        Hour = grp.Key,
        Count = grp.Count()
    });

where dt is your DataTable

EDIT: The above should work as long as you have data for a single date in your DataTable, but if you have rows for multiple dates then grouping should be done on Date as well as Hour like:

var query = dt.AsEnumerable()
    .GroupBy(row => new
    {
        Date = row.Field<DateTime>("dateAndTime").Date, 
        Hour = row.Field<DateTime>("dateAndTime").Hour
    })
    .Select(grp => new
    {
        Date = grp.Key.Date,
        Hour = grp.Key.Hour,
        Count = grp.Count()
    });

score:4

I assume that the column really stores DateTimes, you can group by Date + Hour:

 var hourGroups = dataTable.AsEnumerable()
    .Select(row => new { DateAndTime = row.Field<DateTime>("dateAndTime"), Row = row })
    .GroupBy(x => new { Date = x.DateAndTime.Date, Hour = x.DateAndTime.Date.Hour });

foreach (var x in hourGroups)
    Console.WriteLine("Date: {0} Hour: {1}: Count: {2} All names: {3}", 
        x.Key.Date.ToShortDateString(),
        x.Key.Hour,
        x.Count(),
        string.Join(",", x.Select(xx => xx.Row.Field<string>("name")))); // just bonus

Related Articles