NHibernate Criteria API错误,创建别名和投影集合

本文关键字:别名 投影 集合 创建 Criteria API 错误 NHibernate | 更新日期: 2023-09-27 18:25:39

我的任务是使用NHibernate Criteria API生成成员数据列表。我需要在UI中结合基于复选框的分页和过滤。更复杂的是,我不仅仅从一个实体获取数据,而且这个实体与其他实体有HasMany关系。

我目前有一个错误:

Error executing multi Criteria: 
[SELECT DISTINCT TOP 1000 this_MemberID as y0_ 
 FROM _Members
 Left Outer Join _SubMemberTerms ON _SubMemberTerms.MemberID = this_.MemberID
 Left Outer Join _MemberTerms ON _MemberTerms.MemberID = this_.MemberID
 Left Outer Join _ScriptOption ON _ScriptOption.ID = _MemberTerms.ScriptOpID
 WHERE _MagazineID = 100
 AND 
    (_ScriptOption.MagID IN (1234,5678,9101,.....) 
        OR _ScriptOption.MemberID IS NULL
        OR _ScriptOption.Active = 0)
 AND
    (_SubMemberTerms._ScriptOpID IN (1234,5678,9101,.....)
        OR _SubMemberTerms.ID IS NULL);]

内部异常为:值"5554302"不是类型''"MyBusiness.MemberInfo''",并且不能在此泛型中使用收集参数名称:value。

现在我只是看会员的ID,因为我对我的投影列表中的其他投影都进行了评论。否则,错误会显示"值''"System.object[]''"…

我的标准代码:

var filteredList = CurrentSession.CreateCriteria<MemberInfo>("this_")
                   .SetProjection(Projections.Distinct(Projections.ProjectionList()
                   .Add(Projections.Alias(Projections.Property("ManagedMemberID"), "MemberID"))))
                   .Add(Restrictions.Eq("_MagazineID", (int)magID))
                   .CreateAlias("MemTermsList", "_MemberTerms", NHibernate.SqlCommand.JoinType.LeftOuterJoin)
                   .CreateAlias("_MemberTerms.ScriptOpInfo", "_ScriptOption", NHibernate.SqlCommand.JoinType.LeftOuterJoin)
                   .CreateAlias("SubTermsList", "_SubMemberTerms", NHibernate.SqlCommand.JoinType.LeftOuterJoin)
                   .SetFirstResult(startposition)
                   .SetMaxResults(1000);

我还有一个Criteria,用于获取总查询的计数,而不仅仅是返回的1k。它和我为filteredList制作的Criteria是一样的。

//code for how I'm adding in the MagID's and ScriptOpId's
Disjunction magDisjunction = new Disjunction();
Disjunction subTermsDijunction = new Disjunction();
if(TheCheckBoxThatAppliesIsChecked)
{
    magDisjunction.Add(Restrictions.In("_ScriptOption.MagID", (List<int>)selectedMags))
                  .Add(Restrictions.IsNull("_MemberTerms.MemberID"))
                  .Add(Restrictions.Eq("_MemberTerms.Active", false));
    filteredList.Add(magDisjunction);
}
if(TheOtherCheckBoxThatAppliesIsChecked)
{
    subTermsDisjunction.Add(Restrictions.In("_SubMemberTerms.SciptOpID", (List<int>)selectedScriptOp))
                  .Add(Restrictions.IsNull("_SubMemberTerms.SubMemberTermsID"));
    filteredList.Add(subTermsDisjunction);
}
var finishedList = filteredList.Future<MemberInfo>().ToList<MemberInfo>();
var count = listCount.FutureValue<int>().Value;

编辑:我的投影有个错别字。别名,从"MemberID"切换为"ManagedMemberID"。

NHibernate Criteria API错误,创建别名和投影集合

查询filteredlist具有

.SetProjection(Projections.Distinct(Projections.ProjectionList().Add(Projections.Alias(Projections.Property("MemberID"), "MemberID"))))

与相同

.SetProjection(Projections.Distinct(Projections.Property("MemberID")))

这意味着查询CCD_ 6返回CCD_

var finishedList = filteredList.Future<MemberInfo>().ToList<MemberInfo>();

声明filteredList返回MemberInfo,导致错误

注:

CCD_ 9只是一个noop,因为您正在立即调用CCD_。将其更改为

var finishedList = filteredList.Future<int>();
var count = listCount.FutureValue<int>();

获取查询批处理

更新:

我会去掉投影并使用

int count = CriteriaUtil.Clone(query).SetProjection(Projections.RowCount()).FutureValue<int>();
var results = query.Future<MemberInfo>();

View.MemberCount = count.Value;  // executes both sql together here
foreach (MemberInfo result in results)
{
    // do something with the result
}