Using version 2.1.0.4
Since the following code clears the projection before adding the
rowcount as a projection if the target query had Group By clauses
these will be lost and affect the count returned. The rowcount will
be much higher than expected:
public static ICriteria TransformToRowCount(ICriteria criteria)
{
return
TransformToRowCount((CriteriaImpl)criteria.Clone());
}
private static CriteriaImpl TransformToRowCount(CriteriaImpl
criteria)
{
criteria.ClearOrders();
criteria.SetFirstResult(0).SetMaxResults(RowSelection.NoValue).SetProjection(Projections.RowCount());
return criteria;
}
Example:
var criteria = session.CreateCriteria(typeof(Cat))
.SetProjection( Projections.ProjectionList()
.Add( Projections.Avg("Weight") )
.Add( Projections.Max("Weight") )
.Add( Projections.GroupProperty("Color") ))
var itemCountCrit =
CriteriaTransformer.TransformToRowCount(criteria);
// works
var list = criteria
.SetFirstResult(filter.CurrentPageIndex *
filter.ItemsPerPage)
.SetMaxResults(filter.ItemsPerPage)
.Future<T>();
// will return a number that is too high since the
projections above will be stripped
var resultCount = itemCountCrit.FutureValue<int>();
I'm working on a couple of workarounds or fixes any ideas or thoughts
are appreicated.
--
You received this message because you are subscribed to the Google Groups
"nhusers" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/nhusers?hl=en.