如何在集合Nhibernate上添加限制连词
我有一个集合需要查询并添加限制如何在集合Nhibernate上添加限制连词,nhibernate,collections,queryover,Nhibernate,Collections,Queryover,我有一个集合需要查询并添加限制 public class User { public int Id {get;set;} public string Name {get;set;} public IList<Role> Roles {get;set;} } 现在我想在Nhibernate中进行查询,并且如果用户通过获取任何具有Admin角色的角色,我还需要得到结果 Conjunction conjunction = new Conjunction(); Di
public class User {
public int Id {get;set;}
public string Name {get;set;}
public IList<Role> Roles {get;set;}
}
现在我想在Nhibernate中进行查询,并且如果用户通过获取任何具有Admin角色的角色,我还需要得到结果
Conjunction conjunction = new Conjunction();
Disjunction disjunction = new Disjunction();
if (!string.IsNullOrEmpty(search))
{
disjunction.Add(Restrictions.On<User>(e => e.Name)
.IsLike(string.Format("%{0}%", search)));
conjunction.Add(disjunction);
}
IList<User> users = NhSession.QueryOver<User>()
.Where(conjunction)
.OrderBy(x => x.Name).Asc()
.Take(maxResults)
.List();
如果我有一个RoleName的字符串参数并从查询中获取这些记录,如何进行筛选。假设这是一个映射的关联和筛选,您应该能够加入角色:
Role roleAlias = null;
IList<User> users = NhSession.QueryOver<User>()
.Where(conjunction)
.JoinAlias(() => user.Roles, () => roleAlias)
.Where(() => roleAlias.Name == "Admin") // or whatever your restriction is
.OrderBy(x => x.Name).Asc()
.Take(maxResults)
.List();
如何过滤根对象并限制其集合的方法是通过。但因为您的映射似乎是多对多的,所以这并不简单。我建议更改映射,并引入配对对象。。。使用子查询查询将变得非常简单…我应该如何更改映射。这不会干扰我的实体吗?是的,它肯定会改变你的实体,甚至可能会干扰。这里有一些。但是你不必。。。这只是我的建议。