如何在Nhibernate Projections Class的单​​个GroupProperty方法中添加多个列

时间:2016-11-10 10:42:15

标签: c# nhibernate nhibernate-criteria nhibernate-projections

我正在使用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");

1 个答案:

答案 0 :(得分:0)

一般情况下,除了RowCountSumGroupProperty,我们可以使用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>())