Nhibernate - 对别名的空检查查询给出了对象空引用

时间:2016-03-02 15:30:11

标签: c# nhibernate alias queryover

我试图在nhibernate中进行查询,但遇到了问题。

我想收到所有报告(我的项目中的实体),其中包含具有模式'报告的事件,以及没有发生事故的报告(报告可能有事件但不是必须的 - 事件可以为空“

所以即时尝试:

Incident inc = null;

Session.QueryOver<Report>()
    .Left.JoinAlias(r => r.Incident, () => inc)
    .Where(new Disjunction()
        .Add(Restriction.On(() => inc).IsNull)
        .Add(() => inc.Type == "Violence"));

我得到:

  

对象引用未设置为对象的实例。

1 个答案:

答案 0 :(得分:1)

试试这个:

Session.QueryOver<Report>().Left.JoinAlias(r => r.Incident, () => inc)
    .Where(
        Restrictions.Disjunction()
        .Add(Restrictions.On<Report>(r => r.Incident).IsNull)
        .Add(Restrictions.Eq(Projections.Property(() => inc.Type), "Violence"))
    );