This question is pretty much the opposite of this question: Does C# have built-in support for parsing page-number strings?
So given
1,3,5,6,7,8,9,10
Here a slightly modifed Version of RedFilter's version.
It returns a String instead of an Array of Strings, it Removes 0 ,if in the list, it avoids the Exception if only one Value is in the List.
public static string ToRanges(this List ints)
{
ints.Remove(0); // Note: Remove this if you like to include the Value 0
if (ints.Count < 1) return "";
ints.Sort();
var lng = ints.Count;
if (lng == 1)
return ints[0].ToString();
var fromnums = new List();
var tonums = new List();
for (var i = 0 ; i < lng - 1 ; i++)
{
if (i == 0)
fromnums.Add(ints[0]);
if (ints[i + 1] > ints[i] + 1)
{
tonums.Add(ints[i]);
fromnums.Add(ints[i + 1]);
}
}
tonums.Add(ints[lng - 1]);
string[] ranges = Enumerable.Range(0, tonums.Count).Select(
i => fromnums[i].ToString() +
(tonums[i] == fromnums[i] ? "" : "-" + tonums[i].ToString())
).ToArray();
if (ranges.Length == 1)
return ranges[0];
else
return String.Join(",", ranges);
}