using Skip(aNumber).Count() where aNumber is greater than total count of records results in ArgumentOutOfRangeException

Description

Happens with any mapped entity, where aNumber is greater than the number of results:

Session.Query<Entity>().Skip(aNumber).Count()

Or an example case for us in pagination:

var recordsOnThisPage = Session.Query<Entity>().Skip((pageNumber - 1) * recordsPerPage).Take(recordsPerPage).Count()

Will result in:

System.ArgumentOutOfRangeException : Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index

at System.ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument argument, ExceptionResource resource)
at System.Collections.Generic.List`1.System.Collections.IList.get_Item(Int32 index)
at NHibernate.Linq.DefaultQueryProvider.ExecuteQuery(NhLinqExpression nhLinqExpression, IQuery query, NhLinqExpression nhQuery)
at NHibernate.Linq.DefaultQueryProvider.Execute(Expression expression)
at NHibernate.Linq.DefaultQueryProvider.Execute[TResult](Expression expression)
at System.Linq.Queryable.Count[TSource](IQueryable`1 source)

Environment

SQL Server 2008 and SQLite.

Activity

Show:

Alex Zaytsev June 14, 2018 at 5:06 AM

Moved here.

This is SQLite and SQL Server 2012 (and maybe some others) behaviour:

select count from A limit 100 offset 100;
Would be executed like:

select * from (select count from A) limit 100 offset 100;
instead of expected:

select count from (select * from A limit 100 offset 100);

Ricardo Peres February 27, 2016 at 11:01 AM

Are you sure on this? I cannot reproduce it!
Please submit a failing unit test.

Details

Assignee

Reporter

Labels

Components

Affects versions

Priority

Who's Looking?

Open Who's Looking?

Created February 26, 2016 at 9:59 PM
Updated June 14, 2018 at 5:06 AM
Who's Looking?