I have a method that returns the past x days and it currently does the following:
var dates = new List<DateTime>();
for (int i = 0; i < numDays; i++)
{
dates.Add(DateTime.Today.AddDays(-i));
}
return dates;
I feel like there should be a more compact way of doing this, perhaps using LINQ. Suggestions? Also, if I do keep it the way I have it, is DateTime.Today
such that this would be more efficient if I stored it in a variable outside the loop and then called AddDays
on that value within the loop?
Edit: LINQ uses lazy evaluation, right? I'm getting crazy images in my head:
return DateTime.AllDaysInTheHistoryOfTimeEver.Where(day =>
day.BeforeOrOn(DateTime.Today) &&
day.After(DateTime.Today.AddDays(-numDays))
);