簡體   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