[英]NHibernate - How to QueryOver in joined table with restrictions
I am stuck with a SQL query (using NHibernate 4). 我陷入了SQL查询(使用NHibernate 4)。
I have 2 tables (Client and Technology) with many-to-many relationship so I created a junction table called ClientTechnology. 我有2个具有多对多关系的表(客户和技术),所以我创建了一个称为ClientTechnology的联结表。
I am trying to retrieve all the Technologies available (that are non-custom) PLUS all the Technologies available (that are custom) and belong to a given Client. 我正在尝试检索所有可用技术(非定制)加上所有可用技术(定制)并属于给定客户端。
In SQL this is the statement: 在SQL中,这是以下语句:
declare @clientId int = 1
select * from
[dbo].[Technology] t
where t.IsCustom = 0
union
select t.* from
[dbo].[Technology] t
join [dbo].[ClientTechnology] ct
on ct.TechnologyId = t.Id
where t.IsCustom = 1 and ct.ClientId = @clientId
My Fluent Mapping for Client table is: 我的客户端流利表是:
public ClientMap()
{
Id(x => x.Id);
Map(x => x.Name).Not.Nullable();
}
For Technology table is: 对于技术表是:
public TechnologyMap()
{
Id(x => x.Id);
Map(x => x.Name).Not.Nullable();
Map(x => x.IsCustom).Not.Nullable();
HasMany(x => x.ClientTechnologies)
.Access.ReadOnlyPropertyThroughCamelCaseField(Prefix.Underscore)
.Table("ClientTechnology")
.KeyColumn("TechnologyId");
}
and finally the junction table ClientTechnology: 最后是联结表ClientTechnology:
public ClientTechnologyMap()
{
Id(x => x.Id);
Map(x => x.Alias).Not.Nullable();
Map(x => x.IsDeleted).Not.Nullable();
References<Client>(x => x.Client, "ClientId");
References<Technology>(x => x.Technology, "TechnologyId");
}
I a open to different options to achieve this. 我对实现这一目标的各种选择持开放态度。 Assuming I have available a Client object (the ClientId) I could retrieve first a list of Technologies that match the requirement IsCustom = false and then retrieve a list of Technologies that match the requirement IsCustom = true AND "the provided client is the owner of this custom technology" 假设我有一个Client对象 (ClientId),我可以先检索符合要求IsCustom = false的技术列表,然后检索符合要求IsCustom = true的技术列表,并且“提供的客户端是此所有者定制技术”
Within a method public IEnumerable<Technology> GetTechnologies(Client client)
that must return the enumerable of Technology (given a Client instance) 在必须返回技术的可枚举的public IEnumerable<Technology> GetTechnologies(Client client)
中(给定Client实例)
I have tried the following to retrieve globalTechnologies: 我尝试了以下方法来检索globalTechnologies:
var globalTechnologies = _session.QueryOver<Technology>()
.WhereNot(x => x.IsDeleted)
.WhereNot(x => x.IsCustom)
.List();
And the following for customTechnologies whose owner is the client: 对于所有者为客户的customTechnologies,以下内容是:
Technology technology = null;
ClientTechnology clientTechnology = null;
var customTechnologies = _session.QueryOver<Technology>(() => technology)
.JoinAlias(() => technology.ClientTechnologies, () => clientTechnology)
.WhereNot(x => x.IsDeleted)
.Where(x => x.IsCustom)
.Where(clientTechnology.Client == client) //this doesn't compile
.List();
but I don't know how to access the junction table (joined) in order to apply the restriction . 但是我不知道如何访问连接表(已连接)以应用限制 。
Any help would be much appreciated. 任何帮助将非常感激。 Thank you. 谢谢。
In your case, the only problem is, that you do not provide Expression inside of the .Where()
, so this should do the job: 在您的情况下,唯一的问题是,您没有在.Where()
内提供Expression,因此这应该可以完成工作:
// instead of this
// .Where(clientTechnology.Client == client) //this doesn't compile
// use this
.Where(() => clientTechnology.Client == client)
But I would go even farther. 但是我走得更远。 We should be able to creat subquery, which 我们应该能够创建子查询,
Techonology.Id
which are belonging to client. 将仅返回属于客户端的此类Techonology.Id
。 How to create subquery you can see here: 如何创建子查询,您可以在这里看到:
And example with OR 和OR的例子
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.