Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2010-09-02
5
Medium Priority
?
1,753 Views
Last Modified: 2013-11-11
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?
0
Comment
Question by:ToString1
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
LVL 3

Accepted Solution

by:
with earned 668 total points
ID: 33587584
Give this a try,

Foo nextFoo = dataContext.Foos.Top("1").OrderBy(f => f.ID).FirstOrDefault(f => f.ID > firstfoo.ID)

I think that should get you the next foo with an ID greater than "firstfoo", limited to just 1 record.
0
 
LVL 64

Assisted Solution

by:Fernando Soto
Fernando Soto earned 1332 total points
ID: 33587636
Are you looking for the next Foo ID after this, FOO last = dataContext.FOOs.Last();, which has not yet been create?

0
 

Author Comment

by:ToString1
ID: 33587711
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
0
 
LVL 64

Assisted Solution

by:Fernando Soto
Fernando Soto earned 1332 total points
ID: 33587853
Hi ToString1;

Then this should do what you need. Although you can remove the OrderBy method call because it is the index on the table and should already be sorted.

Fernando
// The ID used to get the next ID in the database table
int id = 5;
FOO last = dataContext.FOOs.Where(f => f.ID >= id).OrderBy(f => f.ID).Skip(1).Take(1);

Open in new window

0
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 33587871
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

0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question