Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6684557
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T04:57:16+00:00 2026-05-26T04:57:16+00:00

I am using Linq to Sql against a SQL Server Compact database. I need

  • 0

I am using Linq to Sql against a SQL Server Compact database.
I need a fast way to find the first hole in an integer based column or if none exist the highest number + 1.

If I was doing it using SQL I would do something like this:

SELECT IdLegacy+1 FROM FLUID AS t1
LEFT JOIN FLUID as t2
ON t1.IdLegacy = t2.IdLegacy+1
WHERE t2.IdLegacy IS NULL

Basically I need something similar in Linq to Sql to achieve the same thing. As it will be called on every insert, I need it to be fast and preferable elegant :-D.

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T04:57:16+00:00Added an answer on May 26, 2026 at 4:57 am

    A left outer join looks like this in LINQ to SQL

    from t1 in fluid
    join t2 in fluid on t1.LegacyId + 1 equals t2.LegacyId into t3
    from maybeGap in t3.DefaultIfEmpty()
    where maybeGap == null
    select new { t1 = t1 }
    

    maybeGap now reflects a record that’s a left outer join from fluid. It might be that the LINQ provider for SQL Compact is limited as SQL Compact is very limited but this is the nuts and bolt of it.

    You can test it using this little test case:

    var list = new List<int> { 1, 2, 3, 5 };
    
    var q =
        from x in list
        join y in list on x + 1 equals y into y
        from z in y.DefaultIfEmpty()
        where z == 0
        select x + 1
        ;
    
    foreach (var item in q)
        Console.WriteLine(item);
    

    Prints 4 and 6, just ignore the last as it will always be there and there’s no easy way to prevent that from occurring without using window functions which aren’t supported by SQL Compact.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using LINQ to SQL classes in a project where the database design is
I've been using Linq to SQL for some time now and I find it
Using Linq to sql and server explorer, I mapped to a loginvalidation stored proc.
Using LINQ to SQL, I have an Order class with a collection of OrderDetails.
Using LINQ to SQL db.Products.Where(c => c.ID == 1).Skip(1).Take(1).ToList(); executes SELECT [t1].[ID], [t1].[CategoryID], [t1].[Name],
When using Linq to SQL and stored procedures, the class generated to describe the
I'm using LINQ to SQL and C#. I have two LINQ classes: User and
We are using Linq To SQL with our own data context logic that executes
I have started using Linq to SQL in a (bit DDD like) system which
We are using Linq To SQL with our own data context logic that executes

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.