如果按小时的数据组为空,如何替换零? [英] How to replace zero if data group by hour is null ?

查看:69
本文介绍了如果按小时的数据组为空,如何替换零?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将数据分组为数小时,





I am grouping my data into hours,


public List<Emails> valuefunc()
   {
       List<Emails> o = new List<Emails>();

       var res = from p in email
                 where p.CurrentStatus=="Completed"
                 group p by new { weekday = p.SubmitDate.Value.DayOfWeek,Hour=p.SubmitDate.Value.Hour } into GRP
                 orderby  GRP.Key.weekday
                 select new  Emails
                 {
                   label= GRP.Key.weekday.ToString() ,
                   label2 = " H " +GRP.Key.Hour ,
                   Values=GRP.Count()
                 };


       o.AddRange(res);
       return o;

   }









输出:







output:

label   Hour    Values 
Sunday   H 0       2 
Sunday  H 1       4 
Sunday  H 3       1 
Sunday  H 6       4 
Sunday  H 7      16 
Sunday  H 9      25 
Sunday  H 20     2 
Sunday  H 22     5 
Monday  H 1      7 
Monday  H 2      3 
Monday  H 6      2 





但我的问题是,如果特定小时值为null,我们怎么能得到零计数的那个小时。就像这里H2是null所以它不显示我怎么能得到零,如果为零,则为零。



but my question is if specific hour the value is null how can we get that hour with zero count. like here H2 is null so it does not display how can i get H2 with zero count if null.

推荐答案

几次我读过你的问题...

最后,我想我得到了你想要达到的目的:

Several times i've read your question...
Finally, i think i got what you're trying to achieve:
//created in LinqPad 4.51.03(AnyCPU)
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")
		};
		
	//start date
	DateTime startDate = new DateTime(2014,12,14,6,0,0);
	//end date
	DateTime endDate = new DateTime(2014,12,14,16,0,0);
	int hdiff = ((TimeSpan) (endDate-startDate)).Hours; 
	hdiff++;
	//define Hours; fill
	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();
}

// Define other methods and classes here
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


这篇关于如果按小时的数据组为空,如何替换零?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆