The most efficient and typical solution that I could think of is:
var dates = new DateTime[7];
for (int i = 0; i < 7; i++)
dates[i] = DateTime.Now.AddDays(i);
This will return me seven (7) dates in an array, which is the result I want. I think ruby can do something like this, simply by specifying dots but I can't recall.
However, is there a more efficient approach? Or is there any way to implement this using linq (possibly via the Aggregate
method?), if there is, even if it is not the most efficient solution I would be curious to see.
Ideally it would not require you to re-declare any object instance for the amount of "times" you need though, and allow you to specify DateTime.Now
just once and the number of items in the array/list you want just once.
Thanks
I would use Enumerable.Range
, which is very handy when it comes to generating sequences of data:
var now = DateTime.Now;
var dates = Enumerable.Range(0, 7).Select(n => now.AddDays(n)).ToArray();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With