Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8604203
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T02:30:55+00:00 2026-06-12T02:30:55+00:00

Assume we have the following data in a SQL Server table (sorted by name)

  • 0

Assume we have the following data in a SQL Server table (sorted by name) :

Id   Name        LName
-------------------------
5   Abbas        Dayyan
3   Mohammad     KD
4   Nima         Ahmad
1   Omid         Zangene
2   Pedram       Ahmadi

we have an Id query string and we wanna get the next and previous row (if exists) from Id.

e.g :

the Id query string is 4, so we wanna get Mohammad KD as previous row and Omid Zangene as next row.

Could you please guide me how can do it with LINQ to Entity Framework.

Edit:
In practice the number of table rows is around 1 million.
Table rows didn’t sort by Name by default, wa need to sort them by Name for the result.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T02:30:57+00:00Added an answer on June 12, 2026 at 2:30 am

    How about this?

    var result = (from person in db.People
                  where person.Id == id
                  let ordered = db.People.OrderBy(p => p.Name)                  
                  let reversed = db.People.OrderByDescending(p => p.Name)                  
                  let previous = reversed.SkipWhile(p => p.Id != id).Skip(1).FirstOrDefault()
                  let next = ordered.SkipWhile(p => p.Id != id).Skip(1).FirstOrDefault()
                  select new { previous, next }).First();
    

    Edit: Took the new specifications into account.

    Edit 2: Modified the code to not use LastOrDefault, which doesn’t work with LINQ to Entities.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's assume I have the following file structure: data.py foo = [] bar =
Assume I have the following style table, col1 col2 and col3 have same value
I have a basic recursive table setup in SQL 2008 with the following structure:
Assume I have a table that has the following structure: ================= | Id |
I have the following problem in R: Lets assume the following data frame: a
I have the following situation: assume I have to display the data in the
We have a SQL server 2008 and one of the tables, say table A
Assume the following tables: TABLE: foo ========================= | foo_id | fk_id | name |
Let's assume we have MySQL table events from which we'll select a data. Now
Let's say I have the following hypothetical data structure: create table country ( country_id

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.