When using an order by, conditional projection and SetFirstResult() 
together in a query I get this error:

*"Invalid index 4 for this SqlParameterCollection with Count=4"*

In my experience, this error usually happens when you have one field mapped 
to two properties. but it is not the case in this situation. 

The query works fine until, I pass a value greater than 0 to 
SetFirstResult().

This bug <https://nhibernate.jira.com/browse/NH-2133> logged with 
Nhiberbate seems very simular, but it was fixed 2 years ago.

Any suggestions on how to proceed? Here is a sample of the code:

var query = Session.CreateCriteria<KeepItem>(KeepsafeAlias)
.CreateAlias("Resource", ResourceAlias)
.CreateAlias("Memory", MemoryAlias, JoinType.LeftOuterJoin);

// other code

query.AddOrder
(

Order.Asc
(

Projections.Conditional

(

Restrictions.IsNull(MemoryAlias + ".MinDate"),

Projections.Conditional

(

Restrictions.IsNull(ResourceAlias + ".MinDate"),

Projections.Constant(DateTime.MaxValue),

Projections.Property(ResourceAlias + ".MinDate")

),

Projections.Property(MemoryAlias + ".MinDate")

)

)

);

//other code

query.SetFirstResult(skip);
query.SetMaxResults(take);
return query.List<KeepItem>();

The stack from NHprof is:

System.IndexOutOfRangeException: Invalid index 4 for this 
SqlParameterCollection with Count=4.
   at System.Data.SqlClient.SqlParameterCollection.RangeCheck(Int32 index)
   at System.Data.SqlClient.SqlParameterCollection.GetParameter(Int32 index)
   at 
System.Data.Common.DbParameterCollection.System.Collections.IList.get_Item(Int32
 
index)
   at NHibernate.Type.Int32Type.Set(IDbCommand rs, Object value, Int32 
index)
   at NHibernate.Type.NullableType.NullSafeSet(IDbCommand cmd, Object 
value, Int32 index)
   at NHibernate.Type.NullableType.NullSafeSet(IDbCommand st, Object value, 
Int32 index, ISessionImplementor session)
   at NHibernate.Param.QuerySkipParameterSpecification.Bind(IDbCommand 
command, IList`1 multiSqlQueryParametersList, Int32 
singleSqlParametersOffset, IList`1 sqlQueryParametersList, QueryParameters 
queryParameters, ISessionImplementor session)
   at NHibernate.Param.QuerySkipParameterSpecification.Bind(IDbCommand 
command, IList`1 sqlQueryParametersList, QueryParameters queryParameters, 
ISessionImplementor session)
   at NHibernate.SqlCommand.SqlCommandImpl.Bind(IDbCommand command, 
ISessionImplementor session)
   at NHibernate.Loader.Loader.PrepareQueryCommand(QueryParameters 
queryParameters, Boolean scroll, ISessionImplementor session)

Reply via email to