如何让 NHibernate 加入?

mez*_*oid 3 c# nhibernate fluent-nhibernate createcriteria

我已经使用 Fluent NHibernate 连接了一个商店和员工类,其中 Stores 可以有很多员工,如下所示:

public class Store
{
    public virtual IList<Employee> Employees { get; set; }
    //other store properties
}

public class Employee
{
    public virtual Store Store { get; set; }   
    public virtual bool? SomeStatus1 { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

我需要获取所有员工未将 SomeStatus1 设置为 true 的商店。

我在这里的成功尝试失败了:

Session.CreateCriteria(typeof(Store))
    .Add(Restrictions.Not(Restrictions.Eq("Employees.SomeStatus1", true))
    .List<Store>();
Run Code Online (Sandbox Code Playgroud)

知道我该怎么做吗?

我的尝试失败的原因是因为列表 Employees 没有 SomeStatus1 的属性......这是相当明显的。

我不知道的是如何让 NHibernate 只获得在我正在寻找的州有员工的商店......

我想我想问 NHibernate 是做一个加入......但我不知道如何要求它这样做......

Luk*_*fer 5

您通过创建子条件加入

var criteria = Session.CreateCriteria(typeof(Store));
var join = criteria.CreateCriteria("Employees");
join.Add(Restrictions.Not(Restrictions.Eq("SomeStatus1", true));
return criteria.List<Store>();
Run Code Online (Sandbox Code Playgroud)

未经测试(obv)希望它有效,但你明白了。这就是我用 N:1 做的,但你有 1:N

编辑:好的,我在发布后做了一些研究。看起来我所做的代码应该可以工作,但会导致员工集合的加载。在ayende 的博客上可以找到相同的基本代码。那里有一个示例,它在不导致重新加载集合的情况下执行相同的操作。希望有帮助。