Solved

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

Posted on 2010-09-02
5
1,642 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

785 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