Get element page number Get element page number oracle oracle

Get element page number


First off, if you don't provide any ordering then the entire concept of an index is meaningless. Next, there is no simple, easy way to get what you want; it will need to be somewhat dependent on the specific query.

If, say, you're ordering the data based on created by date you could do something like this:

var someItem = queryToFetchIndividualItem;var indexOfItem = table.OrderBy(item => item.CreatedDate)//any other filtering provided on your query goes here, i.e. other where statements.Where(item => item.CreateDate < someItem.CreatedDate).Count()

if you're sorting on a "Title" then you could do something like this:

var indexOfItem = table.OrderBy(item => item.Title)//any other filtering provided on your query goes here, i.e. other where statements.Where(item => item.Title < someItem.Title).Count()

I assume you see the pattern here. If we get the count of items that would come before the interesting item in the query we get the index. The database can optimize the query for the fact that we're only interested in the count.


You could use the indexed overload of select (http://msdn.microsoft.com/en-us/library/bb534869.aspx):

var indexed = myDataSource.Select( (x,n) => new { Value = x, RowNumber = n } );

This gives you an enumeration of anonymous classes that contain the row number. From there, you can get a specific record:

var specificRecord = indexed.FirstOrDefault( x => x.Value.Key == "SomeKey" );

Assuming that doesn't return null (i.e., your key was found), you can then get the page it's on with some simple math:

var page = (int)Math.Floor( (double)specificRecord.RowNumber / pageSize );

Note: you will have to test this method to see if it gets handled in the database, or has to pull your ten thousand records into memory; I've only tested it without a database, so it may not be very efficient. If this does not work, then I recommend creating a stored procedure in your database that returns the row and page number.


I abandoned this idea, because i could not implement it with Nhibernate Linq.

But I use easier variant.

I use search as quick filter - display all found data in grid, without going to the page of first found element.

Thx for all replies and comments.