string tags = \"9,3,12,43,2\"
List TagIds = tags.Split(\',\');
This doesn\'t work cause the split method returns a string[]
I made a modification to khalid13's answer. If the user put a string of "0", his answer would remove that from the resulting list. I did something similar but used an anonymous object.
var result = commaSeparatedString.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries)
.Select(s => new { didConvert = int.TryParse(s.TrimNullProtection(), out convertedInt), convertedValue = convertedInt })
.Where(w => w.didConvert)
.Select(s => s.convertedValue)
.ToList();
TrimNullProtection is a custom function I made that protects if the string is null.
What the above does is strip out any strings that were not able to be converted with no error. If you need to error if there was a problem with the conversion, then the accepted answer should do the trick.