zoukankan      html  css  js  c++  java
  • linq to entity group by 时间

    CreationTime是DateTime类型

    group by 年/月/日/小时

    group by 年

    (from d in YourData.OrderBy(x => x.CreationTime)
     group d by new
     {
    	 time = new { d.CreationTime.Year }
     } into g
     select new
     {
    	 AverageValue = g.Average(p => p.Value),
    	 CreationTimeStr = g.Key.time.Year,
    	 MaxValue = g.Max(x => x.Value),
    	 MinValue = g.Min(x => x.Value)
     }).ToList();
    

    group by 小时

    (from d in YourData.OrderBy(x => x.CreationTime)
     group d by new
     {
    	 time = new { d.CreationTime.Year, d.CreationTime.Month, d.CreationTime.Day,d.CreationTime.Hour }
     } into g
     select new
     {
    	 AverageValue = g.Average(p => p.Value),
    	 CreationTimeStr = g.Key.time.Year + "-" + g.Key.time.Month + "-" + g.Key.time.Day+" "+g.Key.time.Hour+":00:00",
    	 MaxValue = g.Max(x => x.Value),
    	 MinValue = g.Min(x => x.Value)
     }).ToList();
    

    其他类推

    group by 半小时

    group by 半小时=group by 30分钟

    (from d in YourData.OrderBy(x => x.CreationTime)
     group d by new
     {
    	 time = new { d.CreationTime.Year, d.CreationTime.Month, d.CreationTime.Day,  d.CreationTime.Hour, Minute = (d.CreationTime.Minute / 30) * 30 }
     } into g
     select new
     {
    	 Value = g.Average(p => p.Value),
    	 CreationTimeStr = g.Key.time.Year + "-" + g.Key.time.Month + "-" + g.Key.time.Day + " " + g.Key.time.Hour + ":"+g.Key.time.Minute.ToString().PadRight(2,'0')+ ":00",
    	 MaxValue = g.Max(x => x.Value),
    	 MinValue = g.Min(x => x.Value)
     }).ToList();
    

    group by 半月、半年等等可以类比

    参考资料

    Grouping by every n minutes

  • 相关阅读:
    Python Ethical Hacking
    Python Ethical Hacking
    Python Ethical Hacking
    Python Ethical Hacking
    Python Ethical Hacking
    Python Ethical Hacking
    Python Ethical Hacking
    Arctic Network POJ
    Truck History POJ
    QS Network ZOJ
  • 原文地址:https://www.cnblogs.com/Lulus/p/11765142.html
Copyright © 2011-2022 走看看