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 207349
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T17:43:40+00:00 2026-05-11T17:43:40+00:00

I am looking for a way to retrieve the surrounding rows in a NHibernate

  • 0

I am looking for a way to retrieve the “surrounding” rows in a NHibernate query given a primary key and a sort order?

E.g. I have a table with log entries and I want to display the entry with primary key 4242 and the previous 5 entries as well as the following 5 entries ordered by date (there is no direct relation between date and primary key). Such a query should return 11 rows in total (as long as we are not close to either end).

The log entry table can be huge and retrieving all to figure it out is not possible.

Is there such a concept as row number that can be used from within NHibernate? The underlying database is either going to be SQlite or Microsoft SQL Server.

Edited Added sample

Imagine data such as the following:

Id   Time
4237 10:00
4238 10:00
1236 10:01
1237 10:01
1238 10:02
4239 10:03
4240 10:04
4241 10:04
4242 10:04   <-- requested "center" row
4243 10:04
4244 10:05
4245 10:06
4246 10:07
4247 10:08

When requesting the entry with primary key 4242 we should get the rows 1237, 1238 and 4239 to 4247. The order is by Time, Id.

Is it possible to retrieve the entries in a single query (which obviously can include subqueries)? Time is a non-unique column so several entries have the same value and in this example is it not possible to change the resolution in a way that makes it unique!

  • 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-05-11T17:43:40+00:00Added an answer on May 11, 2026 at 5:43 pm

    “there is no direct relation between date and primary key” means, that the primary keys are not in a sequential order?

    Then I would do it like this:

    Item middleItem = Session.Get(id);
    
    IList<Item> previousFiveItems = Session.CreateCriteria((typeof(Item))
      .Add(Expression.Le("Time", middleItem.Time))
      .AddOrder(Order.Desc("Time"))
      .SetMaxResults(5);
    
    IList<Item> nextFiveItems = Session.CreateCriteria((typeof(Item))
      .Add(Expression.Gt("Time", middleItem.Time))
      .AddOrder(Order.Asc("Time"))
      .SetMaxResults(5);
    

    There is the risk of having several items with the same time.


    Edit

    This should work now.

    Item middleItem = Session.Get(id);
    
    IList<Item> previousFiveItems = Session.CreateCriteria((typeof(Item))
      .Add(Expression.Le("Time", middleItem.Time)) // less or equal
      .Add(Expression.Not(Expression.IdEq(middleItem.id))) // but not the middle
      .AddOrder(Order.Desc("Time"))
      .SetMaxResults(5);
    
    IList<Item> nextFiveItems = Session.CreateCriteria((typeof(Item))
      .Add(Expression.Gt("Time", middleItem.Time))  // greater 
      .AddOrder(Order.Asc("Time"))
      .SetMaxResults(5);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am looking for a way to retrieve the certificate chain of a targeted
I am looking for a way to retrieve the variable name, so I don't
I'm looking for a way to retrieve the equivalent of what's shown on https://www.facebook.com/me/allactivity
Good day, Basically i am looking for a way to retrieve and save a
Looking for a way to retrieve a file size from a web server using
I am looking for a way to retrieve the style from an element that
I am looking for a way to retrieve the current exception without having to
I'm looking for a way to retrieve a collection of DTOs from my WCF
hey guys im really frustrated im looking for a way to retrieve back my
Is there a way to retrieve what the where clauses i have added in

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.