Posts
12
Comments
10
Trackbacks
0
Monday, July 4, 2011
[TIP] Getting Unused Identity Values

What’s the problem?

Given a DB table with an Identity key in SQL Server,  that field could not be repeated.

Well, if you delete a row, the value would be lost.

We are trying to reuse these values.

Solution

LINQ is something that I love, by that I will use it to solve the problem.

To sumup the post, we are going to use a list of integer that will replace our entityset, with the following keys: 1,2,3,5,6,7,9,10,11,15.

Well, how to get a jump in the list:

   1: var query = from n in listado
   2:             where (n > 1 && (listado.IndexOf(n)!=n-1))
   3:             select listado.IndexOf(n) + 1;
   4: int number = query.Tolist().FirstOrDefault();

Hope it helps!!

 

Regards!

Posted On Monday, July 4, 2011 9:50 AM | Comments (1)