创建所有日期的两个日期之间的数组或列表 [英] Create an array or List of all dates between two dates

查看:338
本文介绍了创建所有日期的两个日期之间的数组或列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我生成多系列的图形与沿X轴的时间。

I am generating multi-series graphs with the date along the X-Axis.

的问题是,并不是所有的图形中的系列具有在日期范围内的同一日期。这意味着,如果我选择2月1日至4月30日,一个系列可以有数据,开始于2月1日,但只有通过三月底的推移而另一系列可能对整个日期范围内的数据。

The problem is that not all of the series in the graph have the same dates in the date range. Meaning that if I choose 1 Feb through 30 Apr that one series may have data that starts at 1 Feb but only goes through the end of March but another series may have data for the entire date range.

这偏斜我需要创建图表。去吧,给在查询开始时拍摄的日期范围内,我想生成日期的列表,并填充数据以图形,填充0的那些系列那些没有数据的日期。

This skews the charts I need to create. Go, given the date range taken at the begining of the query I'd like to generate a list of dates and populate the data to be graphed, padding those series with 0's for those dates that have no data.

推荐答案

LINQ:

Enumerable.Range(0, 1 + end.Subtract(start).Days)
          .Select(offset => start.AddDays(offset))
          .ToArray(); 

For循环:

var dates = new List<DateTime>();

for (var dt = start; dt <= end; dt = dt.AddDays(1))
{
   dates.Add(dt);
}

编辑:
作为与在一个时间序列的默认填充值,则可以列举在完整日期范围内的所有的日期,并挑选直接从串联的日期的值(如果存在),或以其他方式的缺省值。例如:

As for padding values with defaults in a time-series, you could enumerate all the dates in the full date-range, and pick the value for a date directly from the series if it exists, or the default otherwise. For example:

var paddedSeries = fullDates.ToDictionary(date => date, date => timeSeries.ContainsDate(date) 
                                               ? timeSeries[date] : defaultValue);

这篇关于创建所有日期的两个日期之间的数组或列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆