开发者

Find hole in sequence using Linq to Sql

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


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.


  1. You can do joins in Linq
  2. Since it seems to me that you are trying to resolve a low level db integrity problem, I would you trigger.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜