Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Populate a list with a specific range of numbers by using LINQ

Tags:

c#

list

range

linq

In order to populate a List<int> with a range of numbers from 1 to n I can use:

for (i=1; i<=n; i++) {    myList.Add(i); } 

Is there any way to achieve the same result by using LINQ inline expressions?

UPDATE

Assume I have a method getMonthName(i) that given the integer returns the name of the month. Can I populate the list directly with month names somehow by using Enumerable

like image 245
CiccioMiami Avatar asked Feb 20 '12 13:02

CiccioMiami


1 Answers

Enumerable.Range(1,12).Select(getMonthName); 
like image 59
rasmusvhansen Avatar answered Sep 29 '22 09:09

rasmusvhansen