我有一个简单的实体称为EmployeeEntity
有属性ID
, Name
, Age
, Organisation
, and Designation
。我只是使用查询来查询数据库
IQuery query = session.CreateQuery(
"select Name, Designation, Age, Organisation FROM EmployeeEntity " +
"group by Name, Designation, Age, Organisation");
IList<EmployeeEntity> employee = query.List<EmployeeEntity>(); // Throws error
但在转换为我的类型时,它抛出异常:
无法执行查询[SQL: SQL 不可用]
with InnerException
:
值“System.Object[]”不是“NHibernateTest.EmployeeEntity”类型,不能在此通用集合中使用。
参数名称:值
尽管使用此查询工作正常:
IQuery query = session.CreateQuery("select e FROM EmployeeEntity e group by e");
IList<EmployeeEntity> employee = query.List<EmployeeEntity>();
但我不想选择所有列,因为我不需要它们。
如果您只需要一组特定的列,请创建一个与您的列一一映射的类。就像这样:
public class EmployeeView
{
public string Name { get; set; }
public string Designation { get; set; }
public int Age { get; set; }
public string Organization { get; set; }
}
然后,您只需将结果转换器添加到查询中
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>();
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)