Published: November 21 2011

Populate ASP.NET dropdown list with month names using LINQ

Using LINQ and the Enumerable.Range method it's possible to populate a dropdown list of month names in a single line.

ddlMonth.Items.AddRange(
    Enumerable.Range(1, 12)
    .Select(x => new ListItem { 
        Text = new DateTimeFormatInfo().GetAbbreviatedMonthName(x), 
        Value = x.ToString() 
    }).ToArray());

 


Need Some ASP.NET Help?

Search fiverr for freelance ASP.NET developers.


Follow me for updates

On Twitter or RSS.


When I'm not coding...

Me and Tina are on a motorcycle adventure around Australia.
Come along for the ride!


Comments


Supported by