C# – Adding projections to an nhibernate query

cnhibernate

What would be the best way to add projections to an nhibernate query that already may or may not have 1 or more projections set? Calling .SetProjection() appears to replace any projections that may already be there.

To give a little background context I am using a version of the paged result extension method found here and I have come to a point where I am passing in a query with a distinct projection already but that projection is stripped for the count criteria because of calling .SetProjection(Projections.RowCountInt64).

Best Answer

I am not sure if it will work, because I can't check it right now, but why don't you use something like a ProjectionList to do this trick?


var criteria = ...
var projectionList = Projection.ProjectionList();

// Add you projections to the projectionList
projectionList.Add(yourQueryProjection);
projectionList.Add(Projections.RowCountInt64());

criteria.setProjection(projectionList);

Related Topic