How am I able to create a list with the last 5 years in it, such as the years 2011
to 2007
. I don't want to hard code the years, but I want the most recent 5 years based on the current year.
Put the last 5 years in your view model and bind to that:
var last5Years = from n in Enumerable.Range(0,5)
select DateTime.Now.Year - n;
DateTime.Now.Year will give you the current year, then you can use a loop
DateTime dt = DateTime.Now;
for(int i = 0; i < 5; i++)
list.Add(dt.Now.Year - i);
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