tags:

views:

3283

answers:

2

What's the best way to write a LINQ query that inserts a record and then returns the primary key of that newly inserted record using C# ?

+5  A: 

The primary key value will be in that property after the SubmitChanges().

MyTable record = new MyTable();
record.Name = "James Curran";
db.MyTable.InsertOnSubmit(record);
db.SubmitChanges();
Console.WriteLine("record inserted as ID : {0}", record.Id);
James Curran
There's a typo on line 3, it should read InsertOnSubmit(record)
Jonathan S.
Thanks... "Fastest gun in the west" problem strike again....
James Curran
A: 

Any field with AutoGeneratedValue=true will be filled in after that record is submitted to the database by dc.SubmitChanges()

GeekyMonkey