NHibernate: returning a strongly typed list having applied an aggregate function

后端 未结 1 835
谎友^
谎友^ 2021-01-25 19:50

I\'d like to have nHibernate return a strongly typed list from a CreateQuery invocation using HQL.

We\'d like a strongly typed list of \"MyType\" returned, but we\'d li

1条回答
  •  一整个雨季
    2021-01-25 20:33

    Here is what you need to do:

    IList reults = Session.CreateQuery("select r.feedname as feedname, count(r.feedurl) as feedcount from rsssubscriptions r group by r.feedname")
                                .SetResultTransformer(NHibernate.Transform.Transformers.AliasToBean(typeof(myobj)))
                                .List();
    

    And then define the MyObj type like this:

    public class MyObj 
    {
        public virtual long feedcount { get; set; }
        public virtual string feedname { get; set; }
    }
    

    The property names of the above type must be the same as the aliases of your returned properties and all properties must be virtual.

    Read the following post on more info on what you can do with the SetResultTransformer() method: http://www.junasoftware.com/blog/nhibernate-setresulttransformer-and-dto.aspx

    0 讨论(0)
提交回复
热议问题