【发布时间】:2018-04-04 19:04:15
【问题描述】:
所以我在选择父实体时遇到问题,因为这也会返回子实体。 目前我有以下实体:
public class Parent
{
public virtual string KeyPart1 { get; set; }
public virtual string KeyPart2 { get; set; }
public virtual string KeyPart3 { get; set; }
public virtual string ParentProperty { get; set; }
public virtual ISet<Child> Children { get; set; } = new HashSet<Child>();
}
public class Child
{
public virtual string KeyPart1 { get; set; }
public virtual string KeyPart2 { get; set; }
public virtual string KeyPart3 { get; set; }
public virtual string KeyPart4 { get; set; }
public virtual string ChildProperty { get; set; }
}
以及实际的查询:
Parent parentAlias = null;
Child childAlias = null;
var query = Session.QueryOver(() => parentAlias)
.Left.JoinAlias(x => x.Children, () => childAlias)
.Where(whereClause);
这会生成一条与此非常相似的 sql 语句:
SELECT
this_.KeyPart1 as KeyPart11_5_4_,
this_.KeyPart2 as KeyPart22_5_4_,
this_.KeyPart3 as KeyPart33_5_4_,
this_.ParentProperty as ParentProperty4_5_4_,
childalias1_.KeyPart1 as KeyPart11_6_6_,
childalias1_.KeyPart2 as KeyPart22_6_6_,
childalias1_.KeyPart3 as KeyPart33_6_6_,
childalias1_.KeyPart4 as KeyPart44_6_6_,
childalias1_.ChildProperty as ChildProperty5_6_6_,
FROM
Parent this_
left outer join
Child childalias1_
on this_.KeyPart1=childalias1_.KeyPart1
and this_.KeyPart2=childalias1_.KeyPart2
and this_.KeyPart3=childalias1_.KeyPart3
WHERE
(SomeWhereClause)
请注意,这将返回 Parent 和 Child 表。当
query.List()
运行时,它将检索所有的父母+每个父母的所有孩子,在最终结果列表中创建重复的父母条目。我只想检索所有的父母或生成类似于以下的 sql 语句:
SELECT
this_.KeyPart1 as KeyPart11_5_4_,
this_.KeyPart2 as KeyPart22_5_4_,
this_.KeyPart3 as KeyPart33_5_4_,
this_.ParentProperty as ParentProperty4_5_4_,
FROM
Parent this_
left outer join
Child childalias1_
on this_.KeyPart1=childalias1_.KeyPart1
and this_.KeyPart2=childalias1_.KeyPart2
and this_.KeyPart3=childalias1_.KeyPart3
WHERE
(SomeWhereClause)
有没有人有任何关于使用 nhibernate 的 QueryOver API 做到这一点的提示?
【问题讨论】:
标签: c# nhibernate orm queryover