Link to home
Start Free TrialLog in
Avatar of ToString1
ToString1

asked on

linq to sql - how to get next highest id in query

In linq say my query returns the first and last foo object

FOO first = dataContext.Foos.First();
FOO last = dataContext.FOOs.Last();

Great!   The FOO table has an autoincrement number which works great.

Now I want to be able to get the next FOO object with the next highest ID, so my query would be something like

FOO nextFoo = dataContext.FOOs  //get the next foo object

So my question is how to get the next FOO object with the next highest ID?
ASKER CERTIFIED SOLUTION
Avatar of with
with

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ToString1
ToString1

ASKER

hi

Each time I want to call the next foo.ID

No they have all been created and also the ID is autoincrement in database
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Even better, this.
// The ID used to get the next ID in the database table
int id = 5;
FOO last = dataContext.FOOs.Where(f => f.ID > id).Take(1);

Open in new window