Link to home
Avatar of Evan Cutler
Evan CutlerFlag for United States of America

asked on

Last record submitted

Greetings,

I am using LINQ with C#

I have a new record that I put in,
but I need the PK that's generated from it
to refer to an associated table...

The PK is autonumbered when the record is put in.

How do I get it?
if your table entity is tab then

here id is your pk

list<tab>=tab.select(x->x.id).max();
is your primary key an "integer"?
Avatar of Evan Cutler

ASKER

yes.
ok...if I do a select x.id
how do I lock in the insert statement into a transraction so I don't accidentally grab another record, my or does the DB.submitchanges() lock it in for me?
SOLUTION
Avatar of disrupt
disrupt
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
ASKER CERTIFIED SOLUTION
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
Found this....
works great:
Public Sub LinqToSqlAdvanced05()

    Console.WriteLine("ContactID is marked as an identity column")
    Dim con = New Contact() With {.CompanyName = "New Era", .Phone = "(123)-456-7890"}

    db.Contacts.Add(con)
    db.SubmitChanges()

    Console.WriteLine()
    Console.WriteLine("The ContactID of the new record is {0}", con.ContactID)

    cleanup130(con.ContactID)

End Sub
thanks guys.