按顺序搜索缺失的数字最简单方法(C#与SQLServer)

这个C#下实现的数字操作

int[] tabSequence = new[] { 1, 2, 3, 7, 8, 9, 12, 15, 16, 17, 22, 23, 32 };

var results = Enumerable.Range(1, tabSequence.Max()).Except(tabSequence);

//results is: 4, 5, 6, 10, 11, 13, 14, 18, 19, 20, 21, 24, 25, 26, 27, 28, 29, 30, 31

这个是sqlserver下实现的操作

SELECT b.akey, (
    SELECT Top 1 akey 
    FROM table1 a 
    WHERE a.akey > b.akey) AS [next] 
FROM table1 AS b 
WHERE (
    SELECT Top 1 akey 
    FROM table1 a 
    WHERE a.akey > b.akey) <> [b].[akey]+1
ORDER BY b.akey

你可能感兴趣的:(c#)