int startmaxid = 0; if (db.LPicture.Any()) { // LPicture Newmode = db.LPicture.Where(n => ).FirstOrDefault(); startmaxid = db.LPicture.Max(q => q.LPictureid); int endmaxid = db.LPicture.Select(s => s.LPictureid).Max(); }