在NHibernate中执行内部联接

时间:2015-07-09 21:06:02

标签: c# nhibernate

在NHibernate中进行查询时,我一直遇到异常。

这是以下查询:

\ff

这是执行它的行。

 //Query tblSentEmails to pull out all of the records according to that lngMergeID
                strQuery =
                    "SELECT strEmailAddress, tblSentEmails.strSubject FROM tblSentEmails INNER JOIN tblRecipients ON tblSentEmails.strEmailSendID=tblRecipients.strEmailSendID WHERE tblSentEmails.lngMergeID ='" +
                    lngMergeID + "' ORDER BY strEmailAddress ASC";

在此之后我不断得到一个例外,说明存在识别错误​​。

这是原始查询:

IQuery qryGetMergedEmails = _session.CreateSQLQuery(strQuery);

请注意lngMergeID =' 11099'仅仅是为了这个例子,我通过参数传递它。

我是否正确运行此查询?

编辑:这是我的映射:

SELECT strEmailAddress, tblSentEmails.strSubject
FROM tblSentEmails INNER JOIN tblRecipients
ON tblSentEmails.strEmailSendID=tblRecipients.strEmailSendID
WHERE tblSentEmails.lngMergeID='11099'
ORDER BY strEmailAddress ASC

没有属性链接到我的收件人表。只需从电子邮件表中获取PK并通过不同的查询处理器添加它。

2 个答案:

答案 0 :(得分:1)

试试这个

var list = session.QueryOver<tblSentEmails >().JoinQueryOver(tblsentsmails =>
tblsentsmails.tblrecipients ).Where(tblsentsmails => tblsentsmails.lngMergeID == 11099)
.OrderBy(tblsentsmails =>tblsentsmails.strEmailAddress ).List();

答案 1 :(得分:0)

没关系,最后使用以下内容:

                    strQuery = "SELECT strEmailAddress, strSubject from tblSentEmails INNER JOIN tblRecipients ON tblSentEmails.strEmailSendID=tblRecipients.strEmailSendID WHERE tblSentEmails.lngMergeID=:mergeID ORDER BY strEmailAddress ASC";
                var qry = _session.CreateSQLQuery(strQuery)
                    .SetParameter("mergeID", lngMergeID)
                    .List();

按预期结束工作。也许不是最好的解决方案,但现在就足够了。