NHibernate Projection与Null多对一

时间:2016-03-05 00:19:15

标签: c# nhibernate fluent-nhibernate

criteria.CreateAlias("ChildObject", "c");
IProjection fullProjection = Projections.ProjectionList()
           .Add(Projections.Property("Item"), "Item")
           .Add(Projections.Property("c.SubItem"), "ChildObject.SubItem")

对象具有属性 ChildObject

ChildObject 有一个属性 SubItem

NHibernate可以使用此投影成功查询并列出所有带有ChildObject的对象。

但是,某些行上的ChildObject可以为null。这个预测似乎只是完全跳过它们。他们从来没有把它变成我的变形金刚。我认为NHibernate使得这些预测在标准中不是空的。

所以我虽然可以通过以下方式超越它:

Projections.Conditional(Restrictions.Eq("ChildObject", null), nullProjection, fullProection)

nullProjection根本没有投射ChildObject,但它只是抱怨....

Both true and false projections must return the same types.

无论如何都要进行此投影并获得可以为空的值吗?或者我将不得不做两个单独的查询?

1 个答案:

答案 0 :(得分:0)

这与左连接而不是内连接有关。

criteria.CreateAlias("ChildObject", "c");

将SQL语句创建为内部联接,但使用....

criteria.CreateAlias("ChildObject", "c", LeftJoin);

强制执行左连接,并在值不存在时进行处理。