我的任务是使用 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\" 类型,不能在此通用集合中使用。参数名称:值。
现在我只是在查看会员 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。这与Criteria
我为filteredList
.
//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;
编辑:我的projections.Alias 中有一个错字,从“MemberID”切换到“ManagedMemberID”。