views:

44

answers:

1

Given the table structure below, how do you project x and y only with nhibernate? How do you list the results, what is the type of the resulting list?

//Table structure, mapped in the traditional way
FunnyTable { 
             x [int] NOT NULL, 
             y [int] NULL, 
             z [int] NOT NULL
           }

//assume criteria is set somewhere else
ProjectionList list = Projections.ProjectionList()
                                 .Add(Projections.Property("x"))
                                 .Add(Projections.Property("y"));
return criteria.SetProjection(list).List<???>??
+4  A: 

It will return a list whose elements are object[]; each element of array is the value of projected property.

You can map the results to a class instead by using result transformer:

criteria.SetResultTransformer(Transformers.AliasToBean(typeof (MyResult)));

The above assumes you have a MyResult class whose property names match the names (or aliases) of properties you've specified in projection.

ChssPly76