重命名 NHibernate 条件



我正在尝试多次连接别名,但这取决于它应该这样做的次数。

Employee employeeAlias = null;
Thing thingAlias = null;
var names = string[] {"A", "B", "C", "D", "E"} // number of values could vary
for(int i = 0; i < names.Length ; i++)
{
   queryOver
       .JoinAlias(() => employeeAlias.Things, 
                  () => thingAlias,
                  JoinType.LeftOuterJoin, 
                  Restrictions.Eq(Projections.Property(() => thingAlias.Name, 
                                  names[i]));
}

但是,在执行时,NHibernate会抛出一个错误,指出:

NHibernate.QueryException: duplicate alias: thingAlias ---> System.ArgumentException: An item with the same key has already been added

有没有办法让 NHibernate 加入具有指定名称的别名,这样它就不会重用变量的名称?

谢谢大家。从昨天开始,这一直是我的问题,我找不到合适的解决方案。

附注:

我正在尝试做这样的事情:

SELECT * FROM Employee e
LEFT OUTER JOIN Thing t on e.Id = t.EmployeedId AND t.Name = "A"
LEFT OUTER JOIN Thing t on e.Id = t.EmployeedId AND t.Name = "B"

你想实现的是不可能的。每个关系(many-to-oneone-to-many)只能使用一次。

为了支持这一说法,请在这里查看:CriteriaQueryTranslator.cs

private void CreateAssociationPathCriteriaMap()
{
    foreach (CriteriaImpl.Subcriteria crit in rootCriteria.IterateSubcriteria())
    {
        string wholeAssociationPath = GetWholeAssociationPath(crit);
        try
        {
            associationPathCriteriaMap.Add(wholeAssociationPath, crit);
        }
        catch (ArgumentException ae)
        {
            throw new QueryException("duplicate association path: "
                                    + wholeAssociationPath, ae);
        }

因此,我们在这里可以看到的是,无论别名如何,最后,所有关联关系(JOINS)都被添加到associationPathCriteriaMap中,这是IDictionary<string, ICriteria>

这意味着,现在有办法让两个相同的键发挥作用......

我没有尝试过这个,但我认为与其使用 for-loopRestrictions.Eq ,我们可以(不确定,但如果您没有尝试过)使用Restrictions.In喜欢-

Restrictions.In(Projections.Property(() => thingAlias.Name, names));

最新更新