如何将数据分组到周? [英] How Can I Group Data Into Week?

查看:108
本文介绍了如何将数据分组到周?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一年的数据。

数据有两列,



DateTime |产品





我如何按周分组这些数据。



比如第1,2,3周......和它的数量。



使用linq表达式。



喜欢在下面我将它分组为小时如何将它分组为星期,



 来自 p  产品其中​​(p.CurrentStatus ==   sold group  p by(p.DateTime.Value .hour) into  Grp 

select new {Hour = Grp.Key,Count = Grp.Count()})







在下面的情况下它给出了错误(这个函数只能从LINQ到Entities调用。)



  VAR  result = 来自 list1   

来自 p 产品其中​​(p.CurrentStatus == sold group p by(SqlFunctions.DatePart( ww,p.DateTime)) into Grp 选择 new {Week = Grp.Key,Count = Convert.ToDouble(Grp.Sum( p = > p.Price))})

join list2 < span class =code-keyword> in


from q in 产品 where (q.CurrentStatus == sold group q by(SqlFunctions.DatePart( ww, q.DateTime)) into Grp select new {Week = Grp.Key,Count = Convert.ToDouble(Grp.Count())})list1.Week上的

等于list2.Week

选择 new AvgClass
{
周= list1.Week,
AvGG =转换。 ToDouble(list1.Count / list2.Count)
};

o.AddRange(结果);

}

解决方案

第一个链接解释了如何在LINQ to Entity中按命令编写组,第二个解释了如何使用Calendar.GetWeekOfYear从DateTime变量中获取周数...

http://msdn.microsoft.com/en-us/library/vstudio/bb896250(v = VS.100)的.aspx [< a href =http://msdn.microsoft.com/en-us/library/vstudio/bb896250(v=vs.100).aspx\"target =_ blanktitle =New Window> ^ ]

http://msdn.microsoft.com /en-us/library/system.globalization.calendar.getweekofyear.aspx [ ^ ]


source.GroupBy(x => SqlFunctions.DatePart(ww,x.MeasurementDa TE));

I have one year data.
The data having two columns,

DateTime | Product


how can i group this data by week.

like week 1,2,3.... and its count.

using linq expressions.

like in following i am grouping it as Hour how can i group it as week,

from p in Products where (p.CurrentStatus == "sold") group p by (p.DateTime.Value.Hour) into Grp 

select new { Hour= Grp.Key, Count = Grp.Count() })




in below case it gives error (This function can only be invoked from LINQ to Entities.)

 var result = from list1 in

              (from p in Products where (p.CurrentStatus == "sold" ) group p by   (SqlFunctions.DatePart("ww", p.DateTime)) into Grp select new { Week = Grp.Key, Count = Convert.ToDouble(Grp.Sum(p => p.Price)) })

              join list2 in
              
              (from q in Products where (q.CurrentStatus == "sold") group q by  (SqlFunctions.DatePart("ww", q.DateTime)) into Grp select new { Week =Grp.Key, Count = Convert.ToDouble(Grp.Count()) })

                     on list1.Week equals list2.Week

                     select new AvgClass
                     {
                         Week = list1.Week,
                         AvGG = Convert.ToDouble(list1.Count / list2.Count)
                     };

        o.AddRange(result);

}

解决方案

The first link has the explanation how to write a group by command in LINQ to Entity, where the second explains how to use Calendar.GetWeekOfYear to get the week number out of DateTime variable...
http://msdn.microsoft.com/en-us/library/vstudio/bb896250(v=vs.100).aspx[^]
http://msdn.microsoft.com/en-us/library/system.globalization.calendar.getweekofyear.aspx[^]


source.GroupBy(x => SqlFunctions.DatePart("ww", x.MeasurementDate));


这篇关于如何将数据分组到周?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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