使用这个问题的公认答案,我已经能够使用 ICriteria 和投影连接两个字段。我结束了
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>();
这会将预期的查询发送到服务器并且正在工作。
我的问题是除了使用 SqlFunction 使用 ICriteria 和投影进行连接之外,是否还有其他方法。