Tuesday 8 September 2009

What are NHibernate Projections

From NHibernate in action:
"Projection is the operation of choosing columns of a table and eliminating duplicate rows from the result. In SQL, the columns to be included are listed in the select clause." - NHibernate in Action, p.381
From the NHibernate documentation:
The class NHibernate.Expression.Projections is a factory for IProjection instances. We apply a projection to a query by calling SetProjection().
IList results = session.CreateCriteria(typeof(Cat))
.SetProjection( Projections.RowCount() )
.Add( Expression.Eq("Color", Color.BLACK) )
.List();
List results = session.CreateCriteria(typeof(Cat))
.SetProjection( Projections.ProjectionList()
.Add( Projections.RowCount() )
.Add( Projections.Avg("Weight") )
.Add( Projections.Max("Weight") )
.Add( Projections.GroupProperty("Color") )
)
.List();
In relational terminology, projection is defined as taking a vertical subset from the columns of a single table that retains the unique rows.
Tuesday 8 September 2009