问题描述:

So I'm trying to do a Select statement with HQL.

The select looks like this:

 queryParams[1] = new QueryParameter("clientObj", client); // Use Client as obj in hql

queryParams[0] = new QueryParameter("clientIDObj", client.ID);

if (!includeClosedSubTasks)

{

// tasks that aren't completed / closed / cancelled

qryString = "select st

from SubTask st

where st.ParentTask.ParentProject.ParentClient=:clientObj

and st.ParentTaskCategory.VisibleToClient=1 "

+ filter + // more ands

"and st.ParentTaskStatus.ID != 3

and st.ParentTaskStatus.ID != 4 and st.ParentTaskStatus.ID != 8

or st.RaisedByClientID=:clientIDObj

order by st.CreateDateTime";

}

In simple terms, it should do this:

select (a AND b AND c) OR (d) ORDER BY .....

However, when I execute this code I get a Query syntax error with this innner exception:

{"Index was out of range. Must be non-negative and less than the size of the collection.\r\nParameter name: index"}

How can I resolve this?

网友答案:

I think the problem is not with the query, but with the queryParams array. You cannot add to empty list items like this:

queryParams[1] = new QueryParameter("clientObj", client); // Use Client as obj in hql

Instead of that, always try to use method .Add of a list.

Try to rewrite the whole block as something like this:

if (!includeClosedSubTasks)
{
    // TODO get session here somehow as you do normally
    var session = NHSession.GetCurrent();

    // tasks that aren't completed / closed / cancelled
    var qryString = @"SELECT st
                    FROM SubTask st
                    WHERE st.ParentTask.ParentProject.ParentClient = :clientObj
                    AND st.ParentTaskCategory.VisibleToClient = 1 "
                    + filter + // more ands
                    @" AND st.ParentTaskStatus.ID NOT IN (3, 4, 8)
                    OR st.RaisedByClientID = :clientIDObj
                    ORDER BY st.CreateDateTime";

    var query = session.CreateSQLQuery(qryString)
        .SetParameter("clientObj", client)
        .SetParameter("clientIDObj", client.ID);

    // return query or whatever you need to do
}
相关阅读:
Top