我有这段代码获取所有已启用的组及其子代。我的问题是,孩子们也可以被禁用,但我不能得到流利的nhibernate只获取全部儿童被启用的组。我认为这是可能的,但如何?NHibernate - 根据子属性筛选出结果
public class Group {
public bool IsDisabled { get; set; }
public string Description { get; set; }
public ICollection<ChildType> Children { get; protected set; }
}
public class ChildType {
public bool IsDisabled { get; set; }
public string Description { get; set; }
}
public IList<Group> Search(string searchString) {
IQueryOver<Group> query = Session.QueryOver<Group>()
.WhereRestrictionOn(x => x.Description).IsInsensitiveLike(searchString, MatchMode.Start)
.Where(x => !x.IsDisabled)
.OrderBy(x => x.Description).Asc
.Fetch(group => group.Children).Eager;
return query
.Cacheable()
.List();
}
编辑:有一个N:儿童和群体之间的M-关系。
以下是我使用的解决方案:
public class Group {
public long Id { get; set; }
public bool IsDisabled { get; set; }
public string Description { get; set; }
public ICollection<ChildType> Children { get; protected set; }
}
public class ChildType {
public long Id { get; set; }
public bool IsDisabled { get; set; }
public string Description { get; set; }
public ICollection<Group> Groups { get; protected set; }
}
public IList<Group> Search(string searchString) {
ChildType child = null;
Group group = null;
Group joinedGroup = null;
var notDisabled = Session.QueryOver.Of<ExaminationType>()
.Where(x => x.IsDisabled)
.JoinAlias(x => x.Groups,() => joinedGroup)
.Where(x => joinedGroup == group)
.Select(x => x.Id);
IQueryOver<Group> query = Session.QueryOver<Group>()
.WhereRestrictionOn(x => x.Description).IsInsensitiveLike(searchString, MatchMode.Start)
.JoinAlias(x => x.ExaminationTypes,() => child)
.WithSubquery.WhereNotExists(notDisabled)
.OrderBy(x => x.Description).Asc;
return query
.Cacheable()
.List();
}
组和孩子之间存在一个N:M关系,这使得子查询有点烦人,但如果我解决了这个问题,我想这是我将要使用的解决方案。 谢谢! – 2011-04-26 15:20:10
对于子查询,您只需使用“选择”来进行投影。 – Vadim 2011-04-26 16:31:09