LINQ。按天分组。如何轻松做到这一点?
我似乎找不到任何关于此的好的参考。我有很多带有日期的 SQL 数据。所以我想制作一个折线图来显示这些数据随时间的变化。如果我想在一段时间内显示它,那么我需要按天分组..但是LOGDATE是完整日期..而不是DAY..
所以我在下面有这个,但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
};
I can't seem to find any good reference on this. I have a lot of data in SQL with dates. So I wanted to make a line chart to show this data over time. If I want to show it over a period of days then I need to group by days.. But the LOGDATE is the full date.. not the DAY..
So I have this below, but LINQ doesn't know what 'DayOfYear' property is...
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
};
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您希望
.Date
获取DateTime
的日期部分而不是DayOfyear
除非您故意尝试将每个日期放入一年中的同一天年入组。You want
.Date
to get the date part of aDateTime
notDayOfyear
unless you are deliberately trying to put the same day of the year from each year into the group.你为什么使用:
而不是仅仅:
我对此不确定,但是使用
group by
子句中的匿名对象可能会弄乱 L2S。Why are you using:
instead of just:
I'm not sure about this, but it's possible using an anonymous object like that in your
group by
clause is messing up L2S.干得好
Here you go
在 EF core 中,您可以使用
DateTime.Date
获取DateTime
值的日期部分。在 EF6 中,您可以使用 DbFunctions.TruncateTime:
In EF core you can use
DateTime.Date
to get the date portion of aDateTime
value.In EF6 you can use DbFunctions.TruncateTime: