2
Answers

How to retrive the last inserted item ID in LINQ

vijay rags

vijay rags

13y
11.6k
1


Hi,

                from the table i need to get the last inserted item id in LINQ. I am new to this...

        I have a table with columns like ID, NAME,DESCRIPTION, SEQUENCE ID,CREATEDBY,CREATED ON. When ever i insert a record i update the SEQUENCE ID with the existing SEQUENCE ID+1. After some time i need to retrive that last inserted SEQUENCE ID. That too using LINQ.

Thanks,
Vijay.
Answers (2)
1
Vulpes
NA 98.3k 1.5m 13y
The query to do that will be something like this:

int id = (from record in table orderby record.SequenceId descending select record.SequenceId).First();

or you could also use:

int id = (from record in table orderby record.SequenceId select record.SequenceId).Last();
0
Suthish Nair
NA 31.7k 4.6m 13y

 How you inserting records, using LINQ?

 If your are inserting records using LINQ, then on after SubmitChanges() you can get it from ".ID"