How do I get the max ID with Linq to Entity?

Solution 1:

Do that like this

db.Users.OrderByDescending(u => u.UserId).FirstOrDefault();

Solution 2:

try this

int intIdt = db.Users.Max(u => u.UserId);

Update:

If no record then generate exception using above code try this

int? intIdt = db.Users.Max(u => (int?)u.UserId);