int[] list = { 1, 2, 3, 5, 8, 9, 10, 11, 12, 13, 15, 16, 19, 21, 22, 23 };
var query = list.OrderBy(p => p).Aggregate<int, List<List<int>>>(null, (m, n) =>
{
if (m == null) return new List<List<int>>() { new List<int>() { n } };
if (m.Last().Last() != n - 1)
{
m.Add(new List<int>() { n });
}
else
{
m.Last().Add(n);
}
return m;
});
string timeStr = "";
foreach (var p in query)
{
if (p.First() == p.Last())
{
timeStr += p.First()+ "、";
}
else
{
timeStr += p.First()+ "-" + p.Last() + "、";
}
}
Console.WriteLine(timeStr);
结果 1-3、5、8-13、15-16、19、21-23、