NHibernate Criteria API错误,包含create别名和一组投影

时间:2012-02-23 16:03:34

标签: c# nhibernate nhibernate-mapping nhibernate-criteria nhibernate-projections

我的任务是使用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,因为我评论了我的投影列表中的所有其他投影。否则错误将声明“The Value \”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;

编辑: 我的预测中有一个拼写错误.Alias,从“MemberID”切换到“ManagedMemberID”。

1 个答案:

答案 0 :(得分:1)

查询filteredlist

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

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

表示查询filteredlist返回MemberID

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

指出filteredList返回MemberInfo,导致错误

注:

Future()只是 noop ,因为您立即致电ToList()。将其更改为

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
}