3

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

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

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

我在这里的可行尝试失败了:

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

知道我该怎么做吗?

我的尝试失败的原因是因为员工列表没有 SomeStatus1 的属性......这很明显。

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

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

4

3 回答 3

5

您通过创建子标准加入

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

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

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

于 2010-07-21T02:31:56.737 回答
1

尝试:

Session.CreateCriteria(typeof(Store))
.CreateAlias("Employees", "e")
.Add(Restrictions.Not(Restrictions.Eq("e.SomeStatus1", true))
.List<Store>();
于 2010-07-21T10:57:25.783 回答
0

我建议您使用 Linq to NHibernate API 而不是 Criteria API。有了它,您的查询将如下所示:

var query = Session.Linq<Store>()
    .Where(store => store.SomeStatus1 != true);

var result = query.ToList();

更多帮助在这里

于 2010-07-21T05:44:55.973 回答