休眠转换结果为词典

问题描述

| 你好 我想如何在nhibernate中有一个特定的输出
var hql = @\"select t1.info1,t2.info2
                    from table1 t1
                    left outer join t1.table2 t2\";
var variable = session.CreateQuery(hql).List();
该查询返回一个包含另一个数组的对象数组。 即:在第一行中,可以通过变量[0] [0]来检索info1 在同一行中,可以通过变量[0] [1]来检索info2 我知道我可以创建一个新的类
class SpecificQuery
{
  public int info1;
  public int? info2
}

and then call :

session.CreateQuery(hql)
.SetResultTransformer(new AliasToBeanResultTransformer(typeof(SpecificQuery))).List();
但是我不想每次想获得特殊回报时都创建一个特定的类。 有人知道该问题的解决方案吗? 例如,IList > [] 在这种情况下,信息1的第一行可以通过变量0检索(对于变量[0] [0] 谢谢     

解决方法

您可以使用AliasToEntityMapResultTranformer
session.CreateQuery(hql)
   .SetResultTransformer(Transformers.AliasToEntityMap).List();
这将返回IDictionary对象的列表。     ,NHibernate提供了可以在这里使用的Tuple类:
var hql = @\"select t1.info1 as First,t2.info2 as Second
                from table1 t1
                left outer join t1.table2 t2\";

// instead of Tuple<String,decimal> you can use other types of course
var variable = session.CreateQuery(hql)
        .SetResultTransformer(new AliasToBeanResultTransformer(
             typeof(NHibernate.Linq.Tuple<String,decimal>)))
        .List<NHibernate.Linq.Tuple<String,decimal>>();
然后,您将使用
variable[0].First
variable[0].Second
    

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...