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.
How to retrive the last inserted item ID in LINQ
3 Answers
Know the answer? Post it. Somebody with the same question will find it here.

VulpesRank #55 · 29.2K reputationPosted Nov 3, 2011, 2:56 AM EDT
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();
FRAiDOON FETRAT26 reputationPosted Jan 4, 2021, 1:55 AM EST
Suthish NairRank #92 · 22.8K reputationPosted Nov 6, 2011, 3:13 AM EST
If your are inserting records using LINQ, then on after SubmitChanges() you can get it from ".ID"