How do I get NHibernate to connect?

I used Fluent NHibernate to connect the store and employee class, where stores can have many employees as follows:

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

I need to get all stores that have employees that don't have SomeStatus1 for true.

My unsuccessful attempt lost:

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

Any idea how I do this?

The reason why my attempt failed was because the Employees list does not have the SomeStatus1 property ... which is pretty obvious.

What I don't know is how to get NHibernate to receive only stores that have employees in the state I'm looking for ...

I think I want to ask NHibernate to make a connection ... but I don't know how to ask him to do this ...

+3
3

,

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

EDIT: , . , , , , . ayende. , , . , .

+4

Try:

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

API Linq NHibernate API . :

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

var result = query.ToList();

.

0

Source: https://habr.com/ru/post/1755649/


All Articles