2

我有这个代码:

if (pertinentDataExists) {        
    List<Entity> operatorList = new List<Operator>();
    List<Entity> newOperatorList = criteria.newOperatorList;
    foreach (Operator operator in operatorList)
    {
        operatorList.Add(operator);
    }
    queryCriteria.CreateCriteria("OperatorList").Add(Restrictions.In("Operator", operatorList));
}

然后:

if (otherPertinentDataExists) {
    Object[] objects = new Object[criteria.newRoleTypeList.Count];                                                                                                                
    for (int  = 0; i < criteria.RoleTypeList.Count; i++) {                                                                                       
        objects[i] = criteria.RoleTypeList[i].Id;   
    }                                                                                       
    queryCriteria.CreateCriteria("OperatorList").Add(Restrictions.In("Role", objects));
}

这些单独工作就好了。但是,如果这两种情况都有要添加到列表中的标准,那么我会得到:

NHibernate.QueryException 消息:重复关联路径:OperatorList

Role如果已创建条件,如何添加限制?

4

1 回答 1

2

有两种选择。首先,如果可能(使用相同的方法),创建 Criteria 并将它们作为局部变量引用:

var operatorListCriteria = queryCriteria.CreateCriteria("OperatorList");

if ( myCondition1 )
{
  operatorListCriteria.Add(...
}
if ( myCondition2 )
{
  operatorListCriteria.Add(...
}

第二,使用“别名”创建标准,然后,稍后,您可以在任何地方找到它们GetCriteriaByAlias(aliasPath)

var aliasPath = "OperatorListAlias";
var operatorListCriteria = queryCriteria.CreateCriteria("OperatorList", aliasPath);
...
// later find them by aliasPath
var subCriteria = queryCriteria.GetCriteriaByAlias(aliasPath);

注意:同时检查GetCriteriaByPath(associationPath)

于 2013-01-04T06:47:06.560 回答