Given the variable
string ids = Request.QueryString["ids"]; // "1,2,3,4,5";
Is there any way to convert it into a List without doing something like
List<int> myList = new List<int>();
foreach (string id in ids.Split(','))
{
if (int.TryParse(id))
{
myList.Add(Convert.ToInt32(id));
}
}
To create the list from scratch, use LINQ:
If you already have the list object, omit the ToList() call and use AddRange:
If some entries in the string may not be integers, you can use TryParse:
One final (slower) method that avoids temps/TryParse but skips invalid entries is to use Regex:
However, this can throw if one of your entries overflows int (999999999999).