简体   繁体   中英

How to add multiple columns inside a single GroupProperty method of Nhibernate Projections Class

I am using Visual Studio 2008. I am trying add multiple columns inside a single GroupProperty method of Nhibernate.Projections Class. But But I am failing to get the required output. I tried something like this.

    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");

Generally, aside with RowCount , Sum and GroupProperty we can use 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")

The alias "ClientName" is repeated few times as an alias .. not sure which could be omitted, so prefer to use them all

And as shown here NHibernate GroupBy and Sum

That could be easily transformed into

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; }
}

with

.SetProjection(projectionList )
.SetResultTransformer(Transformers.AliasToBean<MyDTO>())

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM