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

后端 未结 1 834
谎友^
谎友^ 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<MyObj> 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<MyObj>();
    

    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)
提交回复
热议问题