tags:

views:

387

answers:

2

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).

A: 

I haven't done this so you may have to rework it a bit but I believe you can do something like

.ProjectionCriteria.Add(Projections.RowCountInt64)

Since it may not have any projections set you might want to check what the ProjectionCriteria property is in that case. It may be null.

Spencer Ruport
I am using an ICriteria so I don't actually have .ProjectionCriteria available to me. I suppose I could pass in an actual criteria implementation but then I would need an extension method for each ICreiteria implementation. Any other ideas?
mockedobject
+1  A: 

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);

Adriano Machado