Combining fields using NHibernate forecasts

Using the accepted answer to this question , I was able to combine the two fields using ICriteria and forecasts. I finished with

return session.CreateCriteria<Contact>()
                .CreateAlias("USState","USState", NHibernate.SqlCommand.JoinType.LeftOuterJoin)
                .SetProjection(Projections.ProjectionList()
                     .Add(Projections.Property<Contact>(x=>x.Id), "Id")
                     .Add(Projections.SqlFunction("concat", 
                            NHibernateUtil.String,
                            Projections.Property<Contact>(x=>x.BasicInfo.FirstName),
                            Projections.Constant(' '),
                            Projections.Property<Contact>(x=>x.BasicInfo.LastName)),"Name")
                     .Add(Projections.Property<Contact>(x=>x.BasicInfo.City),"City")
                     .Add(Projections.Property<Contact>(x=>x.USState.Name) ,"State"))
                .Add(Restrictions.Where<Contact>(x => x.Pack == false))
                .AddOrder(new Order(Projections.Property<Contact>(x => x.BasicInfo.FirstName), true))
                .AddOrder(new Order(Projections.Property<Contact>(x => x.BasicInfo.LastName), true))
                .SetResultTransformer(NHibernate.Transform.Transformers.AliasToBean<ContactsViewModel>())
                .List<ContactsViewModel>();

This sends the expected request to the server and works.

My question is, is there any other way besides using SqlFunction for concatenation using ICriteria and projections.

+3
source share

All Articles