Solved

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

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

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 63

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 63

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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
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…

632 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