假设以下课程
class User
{
public virtual int Id { get; private set; }
public virtual ICollection<Department> Departments { get; private set; }
}
class Department
{
public virtual int Id { get; private set; }
}
class UserMap : ClassMap<User>
{
public UserMap()
{
Id(x => x.Id);
HasManyToMany(x => x.Departments)
.Table("UserDepartment")
.ParentKeyColumn("UserId")
.ChildKeyColumn("DepartmentId");
}
}
然后查询 1 或 2
var results = session.QueryOver<User>()
.JoinQueryOver<Department>(x => x.Departments)
.Where(d => d.Id.IsIn(departmentIds))
.List();
查询 1 和 2
User userAlias = null;
var subquery = session.QueryOver<Department>()
.Where(d => d.User.Id == userAlias.Id && d.Id.IsIn(departmentIds))
.Select(Projections.RowCount());
var results = session.QueryOver<User>()
.WithSubquery.WhereValue(departments.Count).Eq(subquery)
.List();