假設下面的類
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();
歡迎的StackOverflow!你有什麼嘗試,什麼不適合你? –
我是FluentNHibernate的新手,所以沒有嘗試過任何東西,因爲我在Google搜索中找不到任何相關內容?在Google搜索的幫助下,我能夠寫出1-1關係的標準,但不適用於上述情況。 – user2534947