NHibernate - filtering out results based on child-property

谁说胖子不能爱 提交于 2019-12-04 12:28:28

You need to use a subquery in order to achieve what you want. In order to do this though you're going to need to add a Group reference to the ChildType entity.

Group group = null;
var childCrit = QueryOver.Of<ChildType>()
        .Where(c => c.Group == group).And(c => c.IsDisabled)
        .Select(c => c.Id);
var query = Session.QueryOver(() => group)
        .WhereRestrictionOn(x => x.Description).IsInsensitiveLike(searchString, MatchMode.Start)
        .Where(x => !x.IsDisabled)
        .WithSubquery.WhereNotExists(childCrit)
        .OrderBy(x => x.Description).Asc
        .Fetch(group => group.Children).Eager;

This will get all groups that aren't disabled and have no disabled children.

public IList<Group> Search(string searchString) {

    Children children = null;

    IQueryOver<Group> query = Session.QueryOver<Group>()
        .WhereRestrictionOn(x => x.Description).IsInsensitiveLike(searchString, MatchMode.Start)
        .Where(x => !x.IsDisabled)
        .JoinAlias(x => x.Children, () => children)
            .Where(x => !x.IsDisabled)
        .OrderBy(x => x.Description).Asc;

    return query
        .Cacheable()
        .List();
}

That should do what you want to do.

Joining an alias will also fetch it for you.

http://www.philliphaydon.com/2011/04/nhibernate-querying-relationships-are-depth/

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!