我正在使用Visual Studio 2008.我正在尝试在单个内部添加多个列 Nhibernate.Projections类的GroupProperty方法。但是我没能得到所需的输出。我试过这样的事情。
ICriteria Criteria = CreateCriteria("BaseCriteria");
projectionList.Add(Projections.GroupProperty(
Projections.SqlFunction("concat",
NHibernateUtil.String, Projections.Property("FirstName"),
Projections.Constant(' '),
Projections.Conditional(Expression.IsNotNull("LastName"),
Projections.Property("LastName"), Projections.Constant(String.Empty)))), "ClientName");
答案 0 :(得分:0)
一般情况下,除了RowCount
,Sum
和GroupProperty
,我们可以使用SqlGroupProjection
var projectionList = Projections.ProjectionList()
.Add(Projections.RowCount(), "RowCount")
.Add(Projections.Sum("Price"), "Price")
.Add(Projections.GroupProperty("StatusId"), "StatusId")
.Add(Projections.SqlGroupProjection(
"LastName + ' ' + FirstName as ClientName",
"LastName + ' ' + FirstName",
new string[] {"ClientName"},
new IType[] {NHibernate.NHibernateUtil.String})
, "ClientName")
别名“ClientName”作为别名重复几次..不确定哪些可以省略,所以更喜欢全部使用
如图所示NHibernate GroupBy and Sum
这很容易转化为
public class MyDTO
{
public virtual int RowCount { get; set; }
public virtual int Price { get; set; } // type depends on SUM result
public virtual int StatusId { get; set; }
public virtual string Name { get; set; }
}
带
.SetProjection(projectionList )
.SetResultTransformer(Transformers.AliasToBean<MyDTO>())