How can I create a list for the last 5 years, for example, years 2011before 2007. I don’t want to hard code these years, but I want the last 5 years to be based on this year.
2011
2007
Put the last 5 years in your view model and bind to it:
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);
Something like that:
List<int> last5Years = new List<int>(); int currentYear = DateTime.Now.Year; for (int i = currentYear - 5; i < currentYear; i++) { last5Years.Add(i); } //databind here