Several times i've read your question...
Finally, i think i got what you're trying to achieve:
void Main()
{
List<email> emails = new List<email>{
new email(new DateTime(2014,12,14,7,0,0), "Completed"),
new email(new DateTime(2014,12,14,7,15,0), "Completed"),
new email(new DateTime(2014,12,14,7,30,0), "Completed"),
new email(new DateTime(2014,12,14,8,5,0), "Incomplete"),
new email(new DateTime(2014,12,14,9,12,0), "Completed"),
new email(new DateTime(2014,12,14,9,52,0), "Completed"),
new email(new DateTime(2014,12,14,10,20,0), "Incomplete"),
new email(new DateTime(2014,12,14,11,28,0), "Completed"),
new email(new DateTime(2014,12,14,12,36,0), "Incomplete"),
new email(new DateTime(2014,12,14,13,44,0), "Completed"),
new email(new DateTime(2014,12,14,13,49,0), "Completed"),
new email(new DateTime(2014,12,14,13,54,0), "Completed"),
new email(new DateTime(2014,12,14,14,52,0), "Incomplete"),
new email(new DateTime(2014,12,14,15,8,0), "Completed"),
new email(new DateTime(2014,12,14,15,15,0), "Incomplete")
};
DateTime startDate = new DateTime(2014,12,14,6,0,0);
DateTime endDate = new DateTime(2014,12,14,16,0,0);
int hdiff = ((TimeSpan) (endDate-startDate)).Hours;
hdiff++;
List<datetime> hours = new List<datetime>();
for(int i=0; i<hdiff;> {
hours.Add(startDate.AddHours(i));
}
var qry = from hr in hours
select new{
Date = hr.ToString("yyyy-MM-dd HH"),
Count = (emails.Count(e=>e.SubmitDate.ToString("yyyy-MM-dd HH")==hr.ToString("yyyy-MM-dd HH") && e.CurrentStatus=="Completed"))
};
qry.Dump();
}
class email
{
private DateTime aSubmitDate;
private string aStatus = "Incomplete";
public email(DateTime _SubmitDate, string _Status)
{
aSubmitDate = _SubmitDate;
aStatus = _Status;
}
public DateTime SubmitDate
{
get{return aSubmitDate;}
set{aSubmitDate = value;}
}
public string CurrentStatus
{
get{return aStatus;}
set{aStatus = value;}
}
}</datetime></datetime></email></email>
Result:
Date Count
2014-12-14 06 0
2014-12-14 07 3
2014-12-14 08 0
2014-12-14 09 2
2014-12-14 10 0
2014-12-14 11 1
2014-12-14 12 0
2014-12-14 13 3
2014-12-14 14 0
2014-12-14 15 1
2014-12-14 16 0