编程语言
首页 > 编程语言> > 首页> C#>如何让NHibernate做一个联接?

首页> C#>如何让NHibernate做一个联接?

作者:互联网

我使用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; }
}

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

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

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

知道我该怎么做吗?

我的尝试失败的原因是因为雇员列表中没有SomeStatus1 …的属性.

我不知道的是如何让NHibernate仅获得那些我正在寻找该州员工的商店…

我想我想问的是NHibernate进行联接…但是我不知道如何要求它进行联接…

解决方法:

您通过创建子条件加入

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’s blog上可以找到相同的基本代码.该处有一个示例,该示例执行相同的操作而不会导致重新加载该集合.希望能有所帮助.

标签:nhibernate,createcriteria,c,fluent-nhibernate
来源: https://codeday.me/bug/20191024/1916780.html