views:

60

answers:

3

Hey there.

I'm searching for a way to auto increment a "Weight" field in my database table using linq to sql.For example if in my database I already have 5 rows with the weights 4,2,7,5,2 , on my new insert in the database the weight should automatically set to 8, because 7 is the largest weight in the set of rows and the next largest one is 8. Please help me, i could not find one single solution for this.

Thanks for the time.

+1  A: 
int nextWeight = db.TableWithWeights.Max(p => p.Weight) + 1;
BFree
Genius, 10x a lot.:D
TestSubject09
+1  A: 
thing.Weight = existingThings.Max(t => t.Weight) + 1;

Should do the trick,

Dan

Daniel Elliott
10x...BFree was faster:P
TestSubject09
+1  A: 

I'm guessing you're not looking for auto-increment functionality present in various DBs. I don't think you'll find a built-in feature that's similar to what you're looking for. Instead, you'll have to implement it in code:

var newObj = new Obj() { Weight = db.Objs.Max(o => o.Weight) + 1 };
JustLoren