如何使用LINQ获取日期范围内的平均值

时间:2013-10-29 08:04:34

标签: c# linq

如果可以在LINQ to Objects语句中完成以下操作,我试图解决。

我有一个字典,其中键作为DateTime(键是多天的值)和double值。我有太多的数据要在图表上绘制,所以想要每5分钟的平均值。

示例输入

01/01/2012 23:53    5
01/01/2012 23:54    2
01/01/2012 23:55    1
01/01/2012 23:56    3
01/01/2012 23:57    4
01/01/2012 23:58    5
01/01/2012 23:59    6
02/01/2012 00:00    2
02/01/2012 00:01    4
02/01/2012 00:02    5

预期输出

01/01/2012 23:55    3
02/01/2012 00:00    4.4

5 个答案:

答案 0 :(得分:7)

使用此helper方法:

static DateTime RoundToNearestInterval(DateTime dt, TimeSpan d)
{
   int f=0;
   double m = (double)(dt.Ticks % d.Ticks) / d.Ticks;
   if (m >= 0.5)
       f=1;            
   return new DateTime(((dt.Ticks/ d.Ticks)+f) * d.Ticks);
}

就像

一样简单
var result = from kvp in data
             let key = RoundToNearestInterval(kvp.Key, TimeSpan.FromMinutes(5))
             group kvp by key into g
             select new { g.Key, Value = g.Average(x => x.Value) };

var result = data.GroupBy(kvp => RoundToNearestInterval(kvp.Key, TimeSpan.FromMinutes(5)), kvp => kvp.Value)
                 .Select(g => new { g.Key, Value = g.Average() });

LINQPad示例:

void Main()
{
    var tmp = new Dictionary<string, int>
    {
        {"01/01/2012 23:53", 5},
        {"01/01/2012 23:54", 2},
        {"01/01/2012 23:55", 1},
        {"01/01/2012 23:56", 3},
        {"01/01/2012 23:57", 4},
        {"01/01/2012 23:58", 5},
        {"01/01/2012 23:59", 6},
        {"02/01/2012 00:00", 2},
        {"02/01/2012 00:01", 4},
        {"02/01/2012 00:02", 5}
    };
    var data = tmp.ToDictionary(d => DateTime.Parse(d.Key), d=>d.Value);

    var result = from kvp in data
                 let key = RoundToNearestInterval(kvp.Key, TimeSpan.FromMinutes(5))
                 group kvp by key into g
                 select new {g.Key, Value = g.Average (x => x.Value) };

    result.ToDictionary(r => r.Key, v => v.Value).Dump();
}

enter image description here

答案 1 :(得分:0)

您的dictionary似乎包含ordered元素,因此我们可以执行以下操作:

var firstDate = yourDict.First().Key;
var output = yourDict.GroupBy(e=> (int)(e.Key - firstDate).TotalMinutes / 5)
                     .ToDictionary(g => g.First().Key
                                       .AddMinutes(g.Average(e=>(e.Key - g.First().Key).TotalMinutes)),
                                   g => g.Average(e=>e.Value));

注意:OP的输入数据使用与en-US不同的cutlure,月份在一天之后。这是进行一些测试的明显点。否则测试不正确。

答案 2 :(得分:0)

这是一个LINQ查询,可以执行您想要的操作,您可以在LINQPad中进行测试:

void Main()
{
    var points = new[]
    {
        new { dt = new DateTime(2012, 1, 1, 23, 53, 00), value = 5 },
        new { dt = new DateTime(2012, 1, 1, 23, 54, 00), value = 2 },
        new { dt = new DateTime(2012, 1, 1, 23, 55, 00), value = 1 },
        new { dt = new DateTime(2012, 1, 1, 23, 56, 00), value = 3 },
        new { dt = new DateTime(2012, 1, 1, 23, 57, 00), value = 4 },
        new { dt = new DateTime(2012, 1, 1, 23, 58, 00), value = 5 },
        new { dt = new DateTime(2012, 1, 1, 23, 59, 00), value = 6 },
        new { dt = new DateTime(2012, 1, 2, 00, 00, 00), value = 2 },
        new { dt = new DateTime(2012, 1, 2, 00, 01, 00), value = 4 },
        new { dt = new DateTime(2012, 1, 2, 00, 01, 00), value = 5 }
    };

    var interval = TimeSpan.FromMinutes(5);
    var averageByInterval =
        from point in points
        let intervalStart = new DateTime(((int)((point.dt.Ticks + interval.Ticks / 2) / interval.Ticks)) * interval.Ticks)
        group point.value by intervalStart into g
        select new { g.Key, average = g.Average() };
    averageByInterval.Dump();
}

输出:

output

答案 3 :(得分:0)

  var data = new Dictionary<DateTime, double>();

  data.Add(new DateTime(2012, 1, 1, 23, 53, 0), 5);
  data.Add(new DateTime(2012, 1, 1, 23, 54, 0), 2);
  data.Add(new DateTime(2012, 1, 1, 23, 55, 0), 1);
  data.Add(new DateTime(2012, 1, 1, 23, 56, 0), 3);
  data.Add(new DateTime(2012, 1, 1, 23, 57, 0), 4);
  data.Add(new DateTime(2012, 1, 1, 23, 58, 0), 5);
  data.Add(new DateTime(2012, 1, 1, 23, 59, 0), 6);
  data.Add(new DateTime(2012, 1, 2, 0, 0, 0), 2);
  data.Add(new DateTime(2012, 1, 2, 0, 1, 0), 4);
  data.Add(new DateTime(2012, 1, 2, 0, 2, 0), 5);

  var result = data.GroupBy(kvp =>
  {
    var dt = kvp.Key;
    var nearest5 = (int)Math.Round(dt.Minute / 5.0) * 5;
    //Add the minutes after inital date creation to deal with minutes=60
    return new DateTime(dt.Year, dt.Month, dt.Day, dt.Hour, 0, 0).AddMinutes(nearest5);
  })
  .Select(g =>
  {
    return new KeyValuePair<DateTime, double>(g.Key, g.Average(row => row.Value));
  });

  foreach (var r in result)
  {
    Console.WriteLine(r.Key + " " + r.Value);
    //  1/01/2012 11:55:00 PM 3
    //  2/01/2012 12:00:00 AM 4.4

  }

答案 4 :(得分:0)

试试这个:

var results = 
    data
        .GroupBy(
            x => (x.Key.Ticks / TimeSpan.TicksPerMinute + 2) / 5,
            x => x.Value)
        .Select(x => new
        {
            Key = new DateTime(x.Key * TimeSpan.TicksPerMinute * 5),
            Value = x.Average()
        });