user1894419
user1894419

Reputation: 21

Nhibernate Criteria For Select As

I would like to add to my criteria a specific column with an "as" addition. Which means: select 0 AS ID from XYZ ...

How can i do it, using a criteria ?

Thanks !

Upvotes: 0

Views: 809

Answers (1)

Radim Köhler
Radim Köhler

Reputation: 123861

NHibernate Projections are the feature you are looking for:

var projections = Projections.ProjectionList();
projections
  .Add(Projections.Property("EntityId"))
  .Add(Projections.Property("Code"))
  .Add(Projections.Constant(0), "ID"); // const projection

var list = session
  .CreateCriteria<MyEntity>()
  .SetProjection(projections) // projections
  .SetResultTransformer(new AliasToBeanResultTransformer(typeof(MyEntity)))
  .List<MyEntity>();

The first two (EntityId, Code) represent mapped properties. The last projection "ID" is const which could be set to mapped or unmapped property "ID"... of MyEntity class

Upvotes: 2

Related Questions