Can I return the 'id' field after a LINQ insert?

When you enter an object into the DB with Linq-to-SQL you can get the id that you just inserted without making another db call.



Answer: After you commit your object into the db the object receives a value in its ID field.

So:

myObject.Field1 = "value";

// Db is the datacontext
db.MyObjects.InsertOnSubmit(myObject);
db.SubmitChanges();

// You can retrieve the id from the object
int id = myObject.ID;

No comments