How can I convert a range of integers to a list of strings using LINQ?
For example, for a range of integers 1-12, the expected result would be “01”, “02”, “03”, …, “12”.
The approach that I came up with incrementally builds a List<string>. Is there a more succinct way to get my desired result?
var numbers = Enumerable.Range(1, 12);
var numberList = new List<string>();
foreach (var item in numbers)
{
string mth = (item.ToString().Length == 1)
? "0" + item.ToString()
: item.ToString();
numberList.Add(mth);
}
ToString can do this for you: