nHibernate QueryOver 复杂连接查询到 Mvc.SelectList

nHibernate QueryOver Complex Join Query to Mvc.SelectList

我正在使用 nHibernate 执行以下操作 SQL 加入查询以填充 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[]>();

感谢任何帮助。 我收到以下错误。

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

问题描述:当前网络请求执行过程中出现未处理的异常。请查看堆栈跟踪以获取有关错误及其在代码中的来源的更多信息。

异常详细信息: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,我确定我可以弄清楚。如果有人有优雅的方法,请随意。