HQL选择带OR

时间:2016-06-14 14:41:31

标签: c# nhibernate hql

所以我正在尝试用HQL做一个Select语句。

选择如下:

        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";
        }

简单来说,它应该这样做:

选择(a AND b AND c)或(d)ORDER BY .....

但是,当我执行此代码时,我得到一个查询语法错误,这个例外是:

  

{“索引超出范围。必须是非负数且小于集合的大小。\ r \ nParameter name:index”}

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

我认为问题不在于查询,而在于queryParams数组。您无法添加以清空列表项,如下所示:

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

而不是那样,总是尝试使用方法。添加列表。

尝试将整个块重写为:

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
}