简体   繁体   中英

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. The working sql is:

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. 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.

Source Error: Line 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:

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. Though if anyone has a elegant way to do so please feel free.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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