Lets say you want to generate a list of Month names in order to bind them to a DropDownList in an asp.net page.
The simplest way i came up with is the following:
DropDownListMonth.DataTextField = "Name";
DropDownListMonth.DataValueField = "Number";
DropDownListMonth.DataSource = Numbers(1,12).Select(Number=> new
{
Number,
Name = (new DateTime(2000,Number,1)).ToString("MMMM")
});
DropDownListMonth.DataBind();
public static IEnumerable<int> Numbers(int from, int to)
{
for (int i = from; i <= to; i++)
{
yield return i;
}
}
So, Linq and anonymous types rocks!