views:

866

answers:

2

Is there any way to use expressions in the Select clause?

E.g.:

select u.Age/2 from User u

I'm having this exception right now:

NHibernate.QueryException: ',' expected in SELECT before:/ [select u.Age/2 from Business.Entities.User u]

A: 

Yes, you can do that. The result of the query will be a List of Object[], where each element of the array is the result of the column calculation. In your example, if the table had 10 rows, then you'd get back a list of 10 items, each with a single-item Object[] containing the age/2 value.

I'd link to the part of the Hibernate docs that describe this behaviour (it has a special name, I can't remember what it is), but hibernate.org seems to be down again.

skaffman
Hmm.. I'm not able to execute this simple query, but actually I'm using NHibernate, maybe this is the problem?I having this exception:NHibernate.QueryException: ',' expected in SELECT before:/ [selectu.Age/2from Business.Entities.User u]
ah well... Hibernate != NHibernate.....
skaffman
+1  A: 

You just want get the result of u.Age divided by two? You can use HQL but I find it's easier to change the formula attribute in the mapping file.

ie:

<property name="HalfAge" formula="Age / 2" />
Spencer Ruport
Thanks for the answer, this was just an example but I will have it in mind
Care to accept this as an answer then? :]
Spencer Ruport