Avatar of Evan Cutler
Evan Cutler
Flag 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?
C#HTMLASP.NET

Avatar of undefined
Last Comment
Evan Cutler

8/22/2022 - Mon
nishant joshi

if your table entity is tab then

here id is your pk

list<tab>=tab.select(x->x.id).max();
daryal

is your primary key an "integer"?
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
Evan Cutler

ASKER
yes.
Evan Cutler

ASKER
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
disrupt

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
ASKER CERTIFIED SOLUTION
Log in to continue reading
Log In
Sign up - Free for 7 days
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Evan Cutler

ASKER
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
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Evan Cutler

ASKER
thanks guys.