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".
query filteredlist
has
.SetProjection(Projections.Distinct(Projections.ProjectionList().Add(Projections.Alias(Projections.Property("MemberID"), "MemberID"))))
which is the same as
.SetProjection(Projections.Distinct(Projections.Property("MemberID")))
which means that the query filteredlist
returns MemberID
but
var finishedList = filteredList.Future<MemberInfo>().ToList<MemberInfo>();
states that filteredList
returns MemberInfo which results in the error
NOTE:
the Future()
is just a noop since you are calling ToList()
immediatly. change it to
var finishedList = filteredList.Future<int>();
var count = listCount.FutureValue<int>();
to get query batching
Update:
I would get rid of the Projection and use
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
}