pun*_*ter 5 linq grouping entity-framework date
我似乎找不到任何关于此的好的参考。我有很多带有日期的 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
};
Run Code Online (Sandbox Code Playgroud)
在 EF core 中,您可以用来DateTime.Date获取值的日期部分DateTime。
在 EF6 中,您可以使用DbFunctions.TruncateTime:
返回给定日期并清除时间部分
var q = from x in dc.ApplicationLogs
let dt = x.LogDate.Date
// EF6:let dt = DbFunctions.TruncateTime(x.LogDate)
group x by dt into g
select new
{
iCount = g.Count(),
strDate = g.Key
};
Run Code Online (Sandbox Code Playgroud)