LINQ。按天分组。如何轻松做到这一点?
我似乎无法找到任何好的参考资料。 SQL中有很多数据和日期。所以我想制作一个折线图来显示这些数据。如果我想显示它在一段时间内,然后我需要按天分组..但是LOGDATE是完整日期..不是天..LINQ。按天分组。如何轻松做到这一点?
所以我有这下面,但LINQ不知道什么DAYOFYEAR“属性...
var q = from x in dc.ApplicationLogs
let dt = x.LogDate
group x by new { dayofyear = dt.Value.DayOfYear } into g
select new
{
iCount = g.Count(),
strDate = g.Key
};
您可以使用DbFunctions.TruncateTime:
与时间部分返回给定的清除之日起
var q = from x in dc.ApplicationLogs
let dt = DbFunctions.TruncateTime(x.LogDate)
group x by dt into g
select new
{
iCount = g.Count(),
strDate = g.Key
};
干杯。花了我一段时间在谷歌搜索这个 - >正是我需要的。 – pfeds 2015-08-27 05:29:37
你为什么要使用:
let dt = x.LogDate
group x by new { dayofyear = dt.Value.DayOfYear } into g
,而不是只:
group x by x.LogDate.Value.DayOfYear into g
我对此不太确定,但是可以在你的group by
子句中使用类似的匿名对象来搞乱L2S。
你想要.Date
获得DateTime
的日期部分,而不是DayOfyear
,除非你故意将每年的同一天放入组中。
在这里你去
let d = new DateTime(n.time.Year, n.time.Month, n.time.Day) group n by d into grp select grp
在LINQ to Entities查询中调用'new DateTime(...)'将失败http://stackoverflow.com/questions/17367649/only-parameterless -constructors-和初始化,被支持的功能于LINQ -to-entiti – jocull 2016-04-06 19:13:55
是否使用LINQ到SQL? – SLaks 2010-06-16 21:53:46
linq2ent ..。像一些日期函数restrcited这就是为什么这是如此困难,我猜 – punkouter 2010-06-17 14:58:51
我可以使用LogDate.Value.Day但不LogDate.Value.ShortDateTime() – punkouter 2010-06-17 14:59:19