Suppose I have the following list:
var strings = new List<string>();
strings.Add("1");
strings.Add("12.456");
strings.Add("Foobar");
strings.Add("0.56");
strings.Add("zero");
Is there some sort of query I can write in Linq that will return to me only the numeric items, i.e. the 1st, 2nd, and 4th items from the list?
-R.
This will return all the strings that are parseable as
doubles as strings. If you want them as numbers (which makes more sense), you could write an extension method:Don’t forget that
double.TryParse()uses your current culture, so it will give different results on different computers. If you don’t want that, usedouble.TryParse(s, NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out result).