代码之家  ›  专栏  ›  技术社区  ›  punkouter

林克按天分组。如何轻松做到这一点?

  •  3
  • punkouter  · 技术社区  · 15 年前

    我似乎找不到任何好的参考资料。我在SQL中有很多带日期的数据。所以我想制作一个折线图来显示这些数据。如果我想在一段时间内展示它,那么我需要按天分组。但是日志日期是完整的日期。不是一天…

    所以我下面有这个,但是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
                    };
    
    4 回复  |  直到 8 年前
        1
  •  2
  •   Mayoturis Gert Arnold    8 年前

    你可以使用 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
            };
    
        2
  •  2
  •   Ian Mercer    14 年前

    你想要 .Date 获取日期部分 DateTime DayOfyear 除非你是故意把一年中的同一天加入这个小组。

        3
  •  0
  •   tzaman    15 年前

    您为什么使用:

        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 条款把L2弄乱了。

        4
  •  0
  •   Marwan Roushdy    13 年前

    干得好

    let d = new DateTime(n.time.Year, n.time.Month, n.time.Day) group n by d into grp select grp