Concatenate fields using NHibernate projections
Using the accepted answer to this question I have been able to concatenate two fields using ICriteria and projections. I have ended up 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开发者_JAVA技巧>(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 is sending the expected query to the server and is working.
My question is if there is any other way besides using SqlFunction to do concatenation using ICriteria and projections.
精彩评论