我叫一个简单的实体EmployeeEntity与性质ID,Name,Age,Organisation,和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
Run Code Online (Sandbox Code Playgroud)
但在转换为我的类型时,它会抛出异常:
无法执行查询[SQL:SQL不可用]
用InnerException:
值"System.Object []"不是"NHibernateTest.EmployeeEntity"类型,不能在此通用集合中使用.
参数名称:value
虽然它使用此查询工作正常:
IQuery query = session.CreateQuery("select e FROM EmployeeEntity e group by e");
IList<EmployeeEntity> employee = query.List<EmployeeEntity>();
Run Code Online (Sandbox Code Playgroud)
但我不想选择所有列,因为我不需要它们.
Vad*_*dim 13
如果您只需要某组列,请创建一个与您的列一对一映射的类.像这样:
public class EmployeeView
{
public string Name { get; set; }
public string Designation { get; set; }
public int Age { get; set; }
public string Organization { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
然后,您只需要在查询中添加结果转换器
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>();
Run Code Online (Sandbox Code Playgroud)
当你查询时select Name, Designation, Age, Organisation...,NHibernate实际上将返回一个IList<object[]>实例.要解决此问题,请尝试重写HQL select new EmployeeEntity(Name, Designation, Age, Organisation)...并向EmployeeEntity类添加适当的构造函数.
| 归档时间: |
|
| 查看次数: |
8818 次 |
| 最近记录: |