gpt4 book ai didi

c# - NHibernate - 根据子属性过滤掉结果

转载 作者:行者123 更新时间:2023-11-30 14:40:05 25 4
gpt4 key购买 nike

我有这段代码可以获取所有已启用的组及其子项。我遇到的问题是 child 也可以被禁用,但我不能让 nhibernate 流畅地只获取启用了 all child 的组。我认为这是可能的,但如何实现呢?

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

编辑: child 和群体之间存在 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();
}

最佳答案

您需要使用子查询来实现您想要的。为此,您需要添加对 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).Eager;

这将获取所有未禁用且没有禁用子项的组。

关于c# - NHibernate - 根据子属性过滤掉结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5790153/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com