Solved

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

Posted on 2010-09-02
5
1,622 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
  • 3
5 Comments
 
LVL 3

Accepted Solution

by:
with earned 167 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 62

Assisted Solution

by:Fernando Soto
Fernando Soto earned 333 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 62

Assisted Solution

by:Fernando Soto
Fernando Soto earned 333 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 62

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This is a video describing the growing solar energy use in Utah. This is a topic that greatly interests me and so I decided to produce a video about it.
A company’s greatest vulnerability is their email. CEO fraud, ransomware and spear phishing attacks are the no1 threat to a company’s security. Cybercrime is responsible for the largest loss of money to companies today with losses projected to r…

932 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now