代码之家  ›  专栏  ›  技术社区  ›  mezoid

我如何让NHibernate加入?

  •  3
  • mezoid  · 技术社区  · 14 年前

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

    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只得到在我寻找的州有雇员的商店。。。

    3 回复  |  直到 14 年前
        1
  •  5
  •   Luke Schafer    14 年前

    通过创建子条件加入

    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 . 那里有一个示例,它执行相同的操作,而不会导致重新加载集合。希望有帮助。

        2
  •  1
  •   Jamie Ide    14 年前

    尝试:

    Session.CreateCriteria(typeof(Store))
    .CreateAlias("Employees", "e")
    .Add(Restrictions.Not(Restrictions.Eq("e.SomeStatus1", true))
    .List<Store>();
    
        3
  •  0
  •   RivieraKid    13 年前

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

    更多帮助 here .