I want to convert a List
to a List
.
Here is my code:
void Convert(List stringList)
{
In case your stringList has a string that can't be parsed then you can mask that with a default error/invalid value of say -1, rather than encountering exception as below:
List stringList = new List();
stringList.AddRange(new string[] { "1", "2", "3", "4", "sdfsf", "7" }); // for illustration
int temp;
var yourIntegerList = stringList.Select(x => int.TryParse(x, out temp) ? int.Parse(x) : -1).ToList(); // -1 used here, can be any integer
// Now you may remove all -1's
yourIntegerList = yourIntegerList.Where(a => a != -1).ToList();