Obtain the Identity of a New Row Using LINQ

It’s actually pretty easy and user friendly to obtain the value of the identity column using LINQ. After you create your object and insert it on submit, you can call the identity column’s property on your object. For instance:

Dim db As New BlogDataContext()
db.BlogPosts.InsertOnSubmit(MyPost)
db.SubmitChanges()
Dim IdentityValue As Integer = MyPost.PostID

In this example, the IdentityValue variable would be assigned to the PostID of the post that was just submitted.

Leave a Reply

Your email address will not be published. Required fields are marked *