从linq组的日期中获取分钟数



我需要按一天中的每分钟对数据进行分组,并获得该分钟内发生的事件的计数。我现在有:

                items.GroupBy(x => new
                {
                    x.date.Minute,
                    x.date.Hour
                })
                .Select(x => new TransferObject
                {
                    Minute = x.Key.Minute,
                    Hour = x.Key.Hour,
                    Count = x.Count(),
                    Day = date
                }).OrderBy(x => x.Hour).ThenBy(x => x.Minute).ToList();

这是我需要的,但问题是我可能没有每分钟的数据点,如果我没有那分钟的数据,我怎么能把0加到Count字段呢?或者,我可以添加分钟数(0…1440),然后添加缺失的值。

<标题>编辑

解决方案当前仅按开始日期分组,但该对象实际上有一个字段end_date。所以基本上现在我有了在那一分钟开始的所有事件,但是我需要得到在那一分钟正在运行的事件的计数。

我的数据包含:

date                    end_date
2015-05-15 09:52:15.650 2015-05-15 09:55:38.097
2015-05-15 09:52:15.633 2015-05-15 09:52:16.097
2015-05-15 09:52:11.633 2015-05-15 09:52:13.047
2015-05-15 09:51:49.097 2015-05-15 09:55:17.687
2015-05-15 09:51:49.087 2015-05-15 09:56:17.510

目前它不使用end_date字段,所以输出是

{Count:2;Hour:9;Minute:51}  
{Count:3;Hour:9;Minute:52}  

我需要所有正在运行的事件,比如

{Count:2;Hour:9;Minute:51}  
{Count:5;Hour:9;Minute:52}
{Count:3;Hour:9;Minute:53}
{Count:3;Hour:9;Minute:54}
{Count:3;Hour:9;Minute:55}
{Count:2;Hour:9;Minute:56}

看来,您想要GroupJoin()而不仅仅是GroupBy(),诸如此类:

  var source = ... // Your query;
  var result = Enumerable
    .Range(0, 1440) // All the minutes that should be in the result
    .GroupJoin(source, 
       minutes => minutes, 
       item => item.Hour * 60 + item.Minute,
       (minutes, items) => new {
          hour = minutes / 60,
          minute = minutes % 60,
          count = items.Count() } ); // Count() can well return 0...

首先你可以生成所有的hours &使用一些简单的LINQ:

var all = Enumerable.Range(0,24)
                    .SelectMany(x => Enumerable.Range(0,60),
                                (x,y) => new {Hr = x, Min=y});
foreach(var a in all)
    Console.WriteLine("{0}:{1}",a.Hr,a.Min); // 00:00 thru 23:59

实例:http://rextester.com/NPNKN82691

然后您可以使用此集合连接到原始结果以形成最终输出:

var all = Enumerable.Range(0,24)
                    .SelectMany(x => Enumerable.Range(0,60),
                               (x,y) => new {Hr = x, Min=y});
var items = new[]{
        new Item{Date = new DateTime(2015,01,01,0,1,0)},
        new Item{Date = new DateTime(2015,01,01,0,1,0)},
        new Item{Date = new DateTime(2015,01,01,0,1,0)},
        new Item{Date = new DateTime(2015,01,01,0,1,0)},
        new Item{Date = new DateTime(2015,01,01,0,1,0)},
        new Item{Date = new DateTime(2015,01,01,0,1,0)},
        new Item{Date = new DateTime(2015,01,01,0,3,0)}
};
var results = all.GroupJoin(items,
                            x => new {Hour = x.Hr,Minute = x.Min},
                            y => new {Hour = y.Date.Hour,Minute = y.Date.Minute},
                            (x,y) => new {Hour = x.Hr, Minute = x.Min, Count = y.Count()});
foreach(var result in results)
   Console.WriteLine("{0:00}:{1:00} = {2}",result.Hour, result.Minute, result.Count);

输出

00:00 = 0
00:01 = 6
00:02 = 0
00:03 = 1
00:04 = 0
//. .剪. .//
23:55 = 0
23:56 = 0
23:57 = 0
23:58 = 0
23:59 = 0

实例http://rextester.com/OAYZ95244


编辑

如果您更改了考虑日期范围的需求,您可以添加一个用于推断小时的新类和方法& &;每项的分钟数:

 // classes used
 public class ExtrapolatedItem
 {
     public int Hour{get;set;}
     public int Minute{get;set;}
 }
public class Item{
    public DateTime Date{get;set;}
    public DateTime EndDate{get;set;}
}
// method 
private static IEnumerable<ExtrapolatedItem> Extrapolate(Item item)
{
    for(var d = item.Date;d<=item.EndDate; d = d.AddMinutes(1))
    {
        yield return new ExtrapolatedItem{ Hour = d.Hour, Minute = d.Minute};
    }
}

可以这样在原始方法中使用:

var items = new[]{
        new Item{Date = new DateTime(2015,01,01,0,1,0),EndDate = new DateTime(2015,1,1,0,5,0)},
        new Item{Date = new DateTime(2015,01,01,0,1,0),EndDate = new DateTime(2015,1,1,0,5,0)},
        new Item{Date = new DateTime(2015,01,01,0,1,0),EndDate = new DateTime(2015,1,1,0,5,0)},
        new Item{Date = new DateTime(2015,01,01,0,1,0),EndDate = new DateTime(2015,1,1,0,5,0)},
        new Item{Date = new DateTime(2015,01,01,0,1,0),EndDate = new DateTime(2015,1,1,0,5,0)},
        new Item{Date = new DateTime(2015,01,01,0,1,0),EndDate = new DateTime(2015,1,1,0,5,0)},
        new Item{Date = new DateTime(2015,01,01,0,3,0),EndDate = new DateTime(2015,1,1,0,5,0)}
};
var results = all.GroupJoin(items.SelectMany(Extrapolate), // extrapolate each item to a list of extrapolated items
                            x => new {Hour = x.Hr,Minute = x.Min},
                            y => new {Hour = y.Hour,Minute = y.Minute},
                            (x,y) => new {Hour = x.Hr, Minute = x.Min, Count = y.Count()});

输出

00:00 = 0
00:01 = 6
00:02 = 6
00:03 = 7
00:04 = 7
00:05 = 7
00:06 = 0
00:07 = 0
//. .剪. .//

实例:http://rextester.com/VOVJCS56741

这应该没有任何join。假设条目中只有一个日期。

var date = items.Select(i => i.date).Min();
Enumerable.Range(0, 60 * 24)
.Select(i => 
    new 
    {
        Minute = i % 60, 
        Hour = i / 60, 
        Day = date,
        Count = items.Where(j => j.date.Hour = i / 60 && j.date.Minute = i % 60).Count()
    });

相关内容

  • 没有找到相关文章

最新更新