NHibernate Criteria API error with create alias and a collection of projections
Pergunta
I'm tasked with generating a list of member data using the NHibernate Criteria API. I need to incorporate paging and filtering based on checkboxes in the UI. What makes this a bit more complex is that I'm not just fetching data from one entity, but this entity has a HasMany
relationship to other entities.
I currently have an error:
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);]
The Inner exception is: The value \"5554302\" is not of type \"MyBusiness.MemberInfo\" and cannot be used in this generic collection. Parameter name: value.
Right now I'm just looking at the Member's ID because I commented every other projection in my projection list out. Otherwise the error would've stated "The Value \"System.object[]\"...
My Criteria Code:
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);
I also have a Criteria
made for getting the count of the total query and not just the 1k returned. It's the same as the Criteria
I made for 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;
EDIT: I had a typo in my projections.Alias, switched form "MemberID", to "ManagedMemberID".
Nenhuma solução correta