NHibernate - 根据子属性过滤结果

发布于 2024-11-03 12:57:24 字数 2132 浏览 5 评论 0原文

我有这段代码获取所有启用的组及其子组。我遇到的问题是子项也可以被禁用,但我无法让 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();
}

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

江南月 2024-11-10 12:57:24

您需要使用子查询才能实现您想要的。为了做到这一点,您需要添加对 ChildType 实体的 Group 引用。

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;

这将获得所有没有残疾并且没有残疾儿童的组。

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.

傲娇萝莉攻 2024-11-10 12:57:24
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();
}

那应该做你想做的事。

加入别名也会为您获取它。

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

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/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文