Hi,
I am facing a problem with LINQ.
Here is the code,
public class TimeObject
{
public DateTime Time { get; set; }
}
private void TestLINQ()
{
List<TimeObject> results = new List<TimeObject>();
results.Add(new TimeObject() { Time = new DateTime(2010, 8, 1, 0, 10, 0)});
results.Add(new TimeObject() { Time = new DateTime(2010, 8, 1, 0, 20, 0)});
results.Add(new TimeObject() { Time = new DateTime(2010, 8, 1, 0, 30, 0)});
results.Add(new TimeObject() { Time = new DateTime(2010, 8, 1, 0, 40, 0)});
results.Add(new TimeObject() { Time = new DateTime(2010, 8, 1, 0, 50, 0)});
results.Add(new TimeObject() { Time = new DateTime(2010, 8, 1, 1, 10, 0)});
results.Add(new TimeObject() { Time = new DateTime(2010, 8, 1, 1, 20, 0)});
results.Add(new TimeObject() { Time = new DateTime(2010, 8, 1, 1, 30, 0)});
results.Add(new TimeObject() { Time = new DateTime(2010, 8, 1, 1, 40, 0)});
results.Add(new TimeObject() { Time = new DateTime(2010, 8, 1, 2, 15, 0)});
results.Add(new TimeObject() { Time = new DateTime(2010, 8, 1, 2, 30, 0)});
var counts = from result in results
group result by result.Time.Date.Hour into groupedResult
select new { Hour = groupedResult.Key, Count = groupedResult.Count() };
foreach (var count in counts)
{
MessageBox.Show(count.Hour + " - " + count.Count);
}
}
The output I expect is 0 - 5, 1 - 4, 2 - 2
But I am always getting 0 - 12. Why it is not grouping by hour?
Please help me. Thanks.