NHibernate - 如何 QueryOver 加入 table 有限制
NHibernate - How to QueryOver in joined table with restrictions
我遇到了 SQL 查询(使用 NHibernate 4)。
我有 2 个 tables(客户端和技术)具有多对多关系,所以我创建了一个名为 ClientTechnology 的联结 table。
我正在尝试检索所有可用技术(非自定义)以及所有可用技术(自定义)并属于给定客户。
在 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
我的 Fluent Mapping 客户端 table 是:
public ClientMap()
{
Id(x => x.Id);
Map(x => x.Name).Not.Nullable();
}
对于技术 table 是:
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");
}
最后是路口[=52=] 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");
}
我愿意接受不同的选择来实现这一点。
假设我有可用的客户端对象(ClientId)
我可以首先检索符合要求的技术列表 IsCustom = false
然后检索符合要求的技术列表
IsCustom = true AND "the provided client is the owner of this custom technology"
在方法 public IEnumerable<Technology> GetTechnologies(Client client)
中必须 return 技术的可枚举(给定一个客户端实例)
我尝试了以下方法来检索 globalTechnologies:
var globalTechnologies = _session.QueryOver<Technology>()
.WhereNot(x => x.IsDeleted)
.WhereNot(x => x.IsCustom)
.List();
以下是其所有者是客户的 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();
但是我不知道如何访问结点table(已加入)以应用限制。
如有任何帮助,我们将不胜感激。谢谢。
在你的情况下,唯一的问题是你没有在 .Where()
中提供表达式,所以这应该可以完成工作:
// instead of this
// .Where(clientTechnology.Client == client) //this doesn't compile
// use this
.Where(() => clientTechnology.Client == client)
但我会走得更远。我们应该能够创建子查询,
- 只会return属于客户的
Techonology.Id
- 然后我们也可以使用 OR 并有一个查询 select 这些人是:
- NOT IsCustom 或
- 属于客户
如何创建子查询你可以在这里看到:
- How to do a QueryOver in Nhibernate on child collection
和 OR 的例子
- Use OR Clause in queryover in NHibernate
- NHibernate QueryOver with WhereRestriction as OR
我遇到了 SQL 查询(使用 NHibernate 4)。
我有 2 个 tables(客户端和技术)具有多对多关系,所以我创建了一个名为 ClientTechnology 的联结 table。
我正在尝试检索所有可用技术(非自定义)以及所有可用技术(自定义)并属于给定客户。
在 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
我的 Fluent Mapping 客户端 table 是:
public ClientMap()
{
Id(x => x.Id);
Map(x => x.Name).Not.Nullable();
}
对于技术 table 是:
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");
}
最后是路口[=52=] 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");
}
我愿意接受不同的选择来实现这一点。 假设我有可用的客户端对象(ClientId) 我可以首先检索符合要求的技术列表 IsCustom = false 然后检索符合要求的技术列表 IsCustom = true AND "the provided client is the owner of this custom technology"
在方法 public IEnumerable<Technology> GetTechnologies(Client client)
中必须 return 技术的可枚举(给定一个客户端实例)
我尝试了以下方法来检索 globalTechnologies:
var globalTechnologies = _session.QueryOver<Technology>()
.WhereNot(x => x.IsDeleted)
.WhereNot(x => x.IsCustom)
.List();
以下是其所有者是客户的 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();
但是我不知道如何访问结点table(已加入)以应用限制。
如有任何帮助,我们将不胜感激。谢谢。
在你的情况下,唯一的问题是你没有在 .Where()
中提供表达式,所以这应该可以完成工作:
// instead of this
// .Where(clientTechnology.Client == client) //this doesn't compile
// use this
.Where(() => clientTechnology.Client == client)
但我会走得更远。我们应该能够创建子查询,
- 只会return属于客户的
Techonology.Id
- 然后我们也可以使用 OR 并有一个查询 select 这些人是:
- NOT IsCustom 或
- 属于客户
如何创建子查询你可以在这里看到:
- How to do a QueryOver in Nhibernate on child collection
和 OR 的例子
- Use OR Clause in queryover in NHibernate
- NHibernate QueryOver with WhereRestriction as OR