[英]nHibernate QueryOver Complex Join Query to Mvc.SelectList
I'm using nHibernate to do the following SQL Join query to populate a Mvc.SelectList but I am having problems. 我正在使用nHibernate进行以下SQL Join查询以填充Mvc.SelectList,但出现问题。 The working sql is:
工作的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
So far I have this but I could be barking up the wrong tree here so feel free to correct me. 到目前为止,我已经有了这个,但是我可能在这里树错了树,所以随时可以纠正我。
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[]>();
Any help appreciated. 任何帮助表示赞赏。 I get the following error.
我收到以下错误。
Object reference not set to an instance of an object. 你调用的对象是空的。
Description: An unhandled exception occurred during the execution of the current web request. 说明:执行当前Web请求期间发生未处理的异常。 Please review the stack trace for more information about the error and where it originated in the code.
请查看堆栈跟踪,以获取有关错误及其在代码中起源的更多信息。
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object. 异常详细信息:System.NullReferenceException:对象引用未设置为对象的实例。
Source Error: Line 165: .SelectList(list => list 源错误: 行165:.SelectList(list => list
Alright I found the problem here. 好吧,我在这里发现了问题。 The line below is wrong:
下面的行是错误的:
.Select(() => string.Concat(tblPerson.Surname, " ", tblPerson.Forename, (tblOfficer.OfficerType == null ? "" : string.Concat(" (", tblOfficer.OfficerType.OfficerTitle, ")"))))
I need to have a valid lambda (is that correct term) expression here so the correct version is: 我需要在这里有一个有效的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[]>();
Now I just need to format it into a MVC SelectList which I'm sure I can figure out. 现在,我只需要将其格式化为一个MVC SelectList,我相信我可以弄清楚。 Though if anyone has a elegant way to do so please feel free.
虽然如果有人有优雅的方法,请随意。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.