var paddedSeries = fullDates.ToDictionary(date => date, date => timeSeries.ContainsDate(date)
? timeSeries[date] : defaultValue);
258
public static IEnumerable<DateTime> GetDateRange(DateTime startDate, DateTime endDate)
{
if (endDate < startDate)
throw new ArgumentException("endDate must be greater than or equal to startDate");
while (startDate <= endDate)
{
yield return startDate;
startDate = startDate.AddDays(1);
}
}
5 回答
LINQ:
For loop:
编辑:对于时间序列中默认值的填充值,您可以枚举完整日期范围内的所有日期,并直接从系列中选择日期值(如果存在),否则选择默认值 . 例如:
我知道这是一个旧帖子,但尝试使用扩展方法:
并像这样使用它
您可以随意选择自己的日期,而不必限制自己到2000年1月 .
我们的常驻大师Jon Skeet有一个很棒的Range Class可以为DateTimes和其他类型做到这一点 .