NHibernate hql元组结果

时间:2015-03-12 09:45:19

标签: c# nhibernate

hql = "select f, b from Foo f, Bar b"

var resultList = session.CreateQuery(hql).List<object[]>();

结果列表项是一个数组,其中[0]的类型为Foo,[1] id为类型栏

有没有办法使用Tuple&lt; Foo,Bar&gt; (或其他泛型类)作为对象[]的通用参数,所以可以跳过转换?

1 个答案:

答案 0 :(得分:5)

是的,你可以,比如:

// The new Tuple<Foo, Bar>(foo, bar) constructor
Type constructor = typeof(Tuple<Foo, Bar>).GetConstructors()[0];

然后

.SetResultTransformer(Transformers.AliasToBeanConstructor(constructor));
.List()

之前

<强>附录

我已经查看了我的代码(因为我已经在一两年前做过这个:)),我注意到我更喜欢

Type constructor = typeof(Tuple<Foo, Bar>).GetConstructor(new[] { typeof(Foo), typeof(Bar) });

有一个非常好的理由:现在(从.NET 4.5.1开始),Tuple<T1, T2>有一个公共构造函数,即new Tuple(T1 t1, T2 t2)。有没有隐式或显式保证,在未来的.NET版本中,Tuple<T1, T2>的无参数公共构造函数将返回&#34;空&#34 ;元组。较长的GetConstructor形式保证了&#34;权利&#34;将始终采用构造函数。