繁体   English   中英

nHibernate QueryOver复杂的联接查询到Mvc.SelectList

[英]nHibernate QueryOver Complex Join Query to Mvc.SelectList

我正在使用nHibernate进行以下SQL Join查询以填充Mvc.SelectList,但出现问题。 工作的SQL是:

SELECT 
    tblUser.oid, 
    (tblPerson.forename + ', ' + tblPerson.surname + ' (' + tblOfficerType.officer_title) + ')'   AS Name
FROM domainfire_officer tblOfficer
INNER JOIN domainfire_officer_type tblOfficerType ON tblOfficer.officer_type = tblOfficerType.oid
INNER JOIN domainfire_person tblPerson ON tblPerson.oid = tblOfficer.person
INNER JOIN core_user tblUser ON tblPerson.[user] = tblUser.oid
ORDER BY tblPerson.surname, tblPerson.forename

到目前为止,我已经有了这个,但是我可能在这里树错了树,所以随时可以纠正我。

Person tblPerson = null;
        Officer tblOfficer = null;
        User tblUser = null;

        var results = session.QueryOver<Officer>(() => tblOfficer)
          .JoinQueryOver(p => p.Person, () => tblPerson)
          .JoinQueryOver(u => u.User, () => tblUser)
          .SelectList(list => list
            .Select(() => tblUser.Oid)
            .Select(() => string.Concat(tblPerson.Surname, " ", tblPerson.Forename, (tblOfficer.OfficerType == null ? "" : string.Concat(" (", tblOfficer.OfficerType.OfficerTitle, ")"))))
          ).List<object[]>();

任何帮助表示赞赏。 我收到以下错误。

你调用的对象是空的。

说明:执行当前Web请求期间发生未处理的异常。 请查看堆栈跟踪,以获取有关错误及其在代码中起源的更多信息。

异常详细信息:System.NullReferenceException:对象引用未设置为对象的实例。

源错误: 行165:.SelectList(list => list

好吧,我在这里发现了问题。 下面的行是错误的:

.Select(() => string.Concat(tblPerson.Surname, " ", tblPerson.Forename, (tblOfficer.OfficerType == null ? "" : string.Concat(" (", tblOfficer.OfficerType.OfficerTitle, ")"))))

我需要在这里有一个有效的lambda(是正确的术语)表达式,所以正确的版本是:

var results = session.QueryOver<Officer>(() => tblOfficer)
  .JoinQueryOver(p => p.Person, () => tblPerson)
  .JoinQueryOver(u => u.User, () => tblUser)
  .SelectList(list => list
    .Select(() => tblUser.Oid)
    .Select(() => tblPerson.Forename)
    .Select(() => tblPerson.Surname)
    .Select(() => tblOfficer.OfficerType.OfficerTitle)
  ).List<object[]>();

现在,我只需要将其格式化为一个MVC SelectList,我相信我可以弄清楚。 虽然如果有人有优雅的方法,请随意。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM