NHibernate - 过滤出基于子属性的结果 [英] NHibernate - filtering out results based on child-property

查看:143
本文介绍了NHibernate - 过滤出基于子属性的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个代码获取所有启用的组和他们的孩子。我的问题是,孩子们也可以被禁用,但我不能流利的nhibernate只获取所有儿童的启用组。我认为这是可能的,但如何?

  public class Group {
public bool IsDisabled {get;组; }
public string描述{get;组; }
public ICollection< ChildType>孩子{get;保护组}
}

public class ChildType {
public bool IsDisabled {get;组; }
public string描述{get;组; }
}

public IList< Group>搜索(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;

返回查询
.Cacheable()
.List();




$ b

编辑:有一个N:M-小孩和小组之间的关系。

以下是我使用的解决方案:

  public class Group {
public long Id {get;组; }
public bool IsDisabled {get;组; }
public string描述{get;组; }
public ICollection< ChildType>孩子{get;保护组}
}

public class ChildType {
public long Id {get;组; }
public bool IsDisabled {get;组; }
public string描述{get;组; }
public ICollection< Group>组{get;保护组}
}

public IList< Group>搜索(string searchString){
ChildType child = null;
Group group = null;
Group joinedGroup = null;
$ b $ 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;

返回查询
.Cacheable()
.List();


解决方案

您需要使用子查询为了达到你想要的。为了做到这一点,你需要添加一个Group引用到ChildType实体。

  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 )。急于;

这将获得所有未被禁用且没有禁用的子组的组。 b $ b

I have this code fetching all enabled Groups with their children. The problem I have is that the children can also be disabled but I can't get fluent nhibernate to only fetch groups where all childrens are enabled. I assume this is possible but how?

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

Edit: There is a N:M-relation between children and groups.

The following is the solution I used:

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

解决方案

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.

这篇关于NHibernate - 过滤出基于子属性的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆