简体   繁体   English

每小时查询一次数据

[英]Query for data every hour

I have a bunch of date data in a sql table like this: 我在像这样的sql表中有一堆日期数据:

2015-01-1 1:00:00, "some data"
2015-01-1 1:25:00, "some data"
2015-01-1 1:40:00, "some data"
2015-01-1 2:00:00, "some data"
2015-01-1 2:30:00, "some data"
2015-01-1 3:30:00, "some data"
            .
            .
            .
2015-01-31 23:00:00, "some data"
2015-01-31 23:50:00, "some data"

I want to select a report which shows the data in month of january(01) every hour and if for example there is no data on this time 1/1/2015 3:30:00 put Null data for example: 我想选择一个报告,每小时以january(01)月份显示数据,例如,如果此时没有数据,则在2015年1月1日3:30:00放置Null数据,例如:

2015-01-1 1:00:00, "some data"
2015-01-1 2:00:00, "some data"
2015-01-1 3:0:00, "Null data"
            .
            .
            .
2015-01-31 23:00:00, "some data"

this is my code: 这是我的代码:

var query = from item in repo.IonogramRepository.GetAll()
                   where month == item.DateTime.Month
                   orderby item.DateTime ascending
                   select new { item .DateTime,item.Data}
                         ;

but as you see this selected all, and I need select data every hour and if don't exist put Null Data. 但正如您所见,这已全部选中,我需要每小时选择一次数据,如果不存在,则输入Null数据。 Any idea? 任何想法?

Thanks in advance. 提前致谢。

This is the solution tha I found with the help of Akont's comment: 这是我在Akont的评论帮助下找到的解决方案:

  static void Main(string[] args)
    {
         //Example to test, I'm working with a Database
        List<Tuple<DateTime, string>> repo = new List<Tuple<DateTime, string>>(){new Tuple<DateTime, string>(new DateTime(2015,1,1,1,0,0),"Some Data" ),
                                                                                  new Tuple<DateTime, string>(new DateTime(2015,1,1,1,25,0),"Some Data" ),
                                                                                  new Tuple<DateTime, string>(new DateTime(2015,1,1,1,40,0),"Some Data" ),
                                                                                  new Tuple<DateTime, string>(new DateTime(2015,1,1,2,0,0),"Some Data" )};


        var query = from  dateTime in  GenerateStaticListOfDateTime(1,2015)
                    join item in repo on dateTime equals item.Item1 into data
                    from item2 in data.DefaultIfEmpty(new Tuple<DateTime, string>(dateTime,"Null data"))
                    select item2;
        foreach (var item in query)
        {
             Console.WriteLine("DateTime:{0} Data:{1}  ",item.Item1,item.Item2);
        }
    }

    public static List<DateTime> GenerateStaticListOfDateTime(int month,int year)
    {
        var result = new List<DateTime>();
        for (int i = 1; i <= DateTime.DaysInMonth(year,month); i++)
        {
            for (int j = 0; j < 24; j++)
            {
                result.Add(new DateTime(year,month,i,j,0,0));
            }
        }
        return result;
    }

I'm pretty sure that this idea can be explained a little more, but this is what I have until now. 我很确定可以进一步解释这个想法,但这是我到目前为止的想法。 Thanks for your help. 谢谢你的帮助。

I'm not really the best one to answer this. 我并不是回答这个问题的最佳人选。 But maybe I can be of use. 但是也许我会有用。 First i believe you have to make a call about whether you want the last entry within the hour or the last. 首先,我认为您必须打电话询问您是要在一个小时内还是在最后一个小时内输入最后一个条目。 That done I believe you can employ the technique found here 我相信您可以采用这里找到的技术

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM