{"id":22065753,"url":"https://github.com/karenpayneoregon/find-gaps-csharp-sqlserver","last_synced_at":"2026-05-03T16:32:55.508Z","repository":{"id":110840607,"uuid":"532539897","full_name":"karenpayneoregon/find-gaps-csharp-sqlserver","owner":"karenpayneoregon","description":"Teaches how to find gaps in a SQL-Server table identity and also with int arrays ","archived":false,"fork":false,"pushed_at":"2023-01-21T16:16:15.000Z","size":72,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-23T18:17:17.233Z","etag":null,"topics":["csharp-code","files","sqlserver"],"latest_commit_sha":null,"homepage":"","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/karenpayneoregon.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-09-04T12:53:07.000Z","updated_at":"2022-09-10T17:07:20.000Z","dependencies_parsed_at":null,"dependency_job_id":"710bc4e7-27f0-408d-852e-ea3b15baa953","html_url":"https://github.com/karenpayneoregon/find-gaps-csharp-sqlserver","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/karenpayneoregon/find-gaps-csharp-sqlserver","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/karenpayneoregon%2Ffind-gaps-csharp-sqlserver","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/karenpayneoregon%2Ffind-gaps-csharp-sqlserver/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/karenpayneoregon%2Ffind-gaps-csharp-sqlserver/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/karenpayneoregon%2Ffind-gaps-csharp-sqlserver/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/karenpayneoregon","download_url":"https://codeload.github.com/karenpayneoregon/find-gaps-csharp-sqlserver/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/karenpayneoregon%2Ffind-gaps-csharp-sqlserver/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":32577122,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-03T06:36:36.687Z","status":"ssl_error","status_checked_at":"2026-05-03T06:36:09.306Z","response_time":103,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["csharp-code","files","sqlserver"],"created_at":"2024-11-30T19:21:47.713Z","updated_at":"2026-05-03T16:32:55.485Z","avatar_url":"https://github.com/karenpayneoregon.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Working with broken Sequences (C#)\n\nIn this article with code samples learn how to find missing elements in sequences.\n\nThere are two example projects\n\n- Find `gaps` identifiers in a SQL-Server table found in the project `DatabaseExampleConsoleApp` which explains how to perform the task of finding `gaps`.\n- Find missing elements (gaps) in an `int` array or from reading data from a file with primary keys and one or more are missing.\n\n# With SQL-Server\n\nWhen working with a SQL-Server database when records are removed with a primary key there become gaps[^1] which can be problematic outside of normal removal of rows. To find out how to query for gaps see the code in `DatabaseExampleConsoleApp` which first executes a select statement to find how many rows are in the table which may have gaps followed by another query to get missing keys.\n\nThe following will create a temp table with gaps and use the query used the project `DatabaseExampleConsoleApp` to get gaps. So this means you can try this out without running example code.\n\n```sql\nDECLARE @BrokenTable TABLE (ID INT);\nINSERT INTO @BrokenTable VALUES (1);\nINSERT INTO @BrokenTable VALUES (3);\nINSERT INTO @BrokenTable VALUES (5);\nINSERT INTO @BrokenTable VALUES (7);\nINSERT INTO @BrokenTable VALUES (9);\nWITH CTE\nAS (SELECT 1 AS Number\n    UNION ALL\n    SELECT Number + 1\n    FROM CTE\n    WHERE Number \u003c= 100)\nSELECT TOP (5) *\nFROM CTE\nWHERE Number NOT IN (SELECT ID FROM @BrokenTable)\nORDER BY Number\nOPTION (MAXRECURSION 0);\n```\n\n# With int array\n\nA basic example were 4, 5, 6, 8, 9, 11, 12, 13 and 14 are missing from the following array\n\n```csharp\nint[] sequence1 = new[] { 1, 2, 3, 7, 10, 15 };\n```\n\nFirst step might be to see if there are gaps, in this case using a language exension\n\n```csharp\n[DebuggerStepThrough]\npublic static bool IsSequenceBroken(this int[] sequence)\n{\n    return sequence\n        .Sort()\n        .Zip(sequence.Skip(1), (valueLeft, valueRight)\n        =\u003e valueRight - valueLeft)\n            .Any(item =\u003e item != 1);\n}\n```\n\nIf there are gaps the following language extension will return them.\n\n```csharp\n[DebuggerStepThrough]\npublic static int[] Missing(this int[] sequence)\n{\n    Array.Sort(sequence);\n    return Enumerable\n        .Range(1, sequence[^1])\n        .Except(sequence)\n        .ToArray();\n}\n```\n\nWe can do the same with a `list` of `int`\n\n```csharp\n[DebuggerStepThrough]\npublic static int[] Missing(this List\u003cint\u003e sequence)\n{\n    Array.Sort(sequence.ToArray());\n    return Enumerable\n        .Range(1, sequence[^1])\n        .Except(sequence)\n        .ToArray();\n}\n```\n\nAnother common type is IEnumerable\u0026lt;int\u003e\n\n```csharp\n[DebuggerStepThrough]\npublic static int[] Missing(this IEnumerable\u003cint\u003e sequence)\n{\n    Array.Sort(sequence.ToArray());\n    return Enumerable\n        .Range(1, sequence.Last())\n        .Except(sequence)\n        .ToArray();\n}\n```\n\n:sparkle: notice in each of the above we sort the type, if we pass say `{ 11, 2, 3, 7, 10, 1 }` in the method will fail as the sequence is out of order.\n\nPerhaps a practical example, reading a list of a specfic type from a file eg.\n\n```csharp\npublic partial class Contacts\n{\n\n    public int ContactId { get; set; }\n    public string FirstName { get; set; }\n    public string LastName { get; set; }\n    public int? ContactTypeIdentifier { get; set; }\n}\n```\n\n- Read in the file as shown in FileOperations.ReadContacts than take the list and use the `Missing` language exension used on a simple `int array` and get missng `ContactId`.\n\n```csharp\npublic static int[] MissingContactIdentifiers()\n{\n    var (_, contacts) = ReadContacts(\"Contacts.csv\");\n    var existingIdentifiers = contacts.Select(x =\u003e x.ContactId).ToList();\n    return existingIdentifiers.Missing();\n}\n```\n\n# Summary\n\nWhat has been presented, finding gaps in integers is something seldom needed but having this `tucked away` in your tool belt can save a developer time by having this code rather than code it.\n\n## See also\n\nThis article provides a way to store `tucked away` code \n[Visual Studio Code stash with VS-Code](https://social.technet.microsoft.com/wiki/contents/articles/54244.visual-studio-code-stash-with-vs-code.aspx)\n\n\n[^1]: The \"gaps and islands\" problem is a scenario in which you need to identify groups of continuous data (`islands`) and groups where the data is missing (`gaps`) across a particular sequence.","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkarenpayneoregon%2Ffind-gaps-csharp-sqlserver","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkarenpayneoregon%2Ffind-gaps-csharp-sqlserver","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkarenpayneoregon%2Ffind-gaps-csharp-sqlserver/lists"}