Error while conversion of Nhibernate query to generic list

后端 未结 2 795
萌比男神i
萌比男神i 2021-01-02 06:10

I have a simple entity called EmployeeEntity with properties ID, Name, Age, Organisation, and Designat

相关标签:
2条回答
  • 2021-01-02 06:49

    If you only want a certain set of columns, create a class that maps one to one with your columns. Like so:

    public class EmployeeView
    {
        public string Name { get; set; }
        public string Designation { get; set; }
        public int Age { get; set; }
        public string Organization { get; set; }
    }
    

    You then just need to add a result transformer to your query

    IQuery query = session
        .CreateQuery("select Name ,Designation ,Age ,Organisation  FROM EmployeeEntity   group by  Name ,Designation ,Age ,Organisation")
        .SetResultTransformer(Transformers.AliasToBean<EmployeeView>());
    
    Ilist<EmployeeEntity> employee= query.List<EmployeeView>();
    
    0 讨论(0)
  • 2021-01-02 06:55

    When you're querying with select Name, Designation, Age, Organisation..., NHibernate will actually return an IList<object[]> instance. To overcome this, try rewriting your HQL to select new EmployeeEntity(Name, Designation, Age, Organisation)... and add an appropriate constructor to EmployeeEntity class.

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