我在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"
我想选择一个报告,每小时显示一月(01)月份的数据,例如,如果此时没有数据,1/1/2015 3:30:00将空数据放在例如:< / p>
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"
这是我的代码:
var query = from item in repo.IonogramRepository.GetAll()
where month == item.DateTime.Month
orderby item.DateTime ascending
select new { item .DateTime,item.Data}
;
但是当你看到这个全部选中时,我需要每小时选择一次数据,如果不存在则输入空数据。有什么想法吗?
提前致谢。
答案 0 :(得分:1)
这是我在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;
}
我很确定这个想法可以多说一点,但这就是我现在所拥有的。谢谢你的帮助。
答案 1 :(得分:0)
我不是最好的回答这个问题的人。但也许我可以使用。首先,我相信你必须打电话询问你是想要在一小时内还是最后一次进入。这样做我相信你可以使用here
找到的技术