hql = "select f, b from Foo f, Bar b"
var resultList = session.CreateQuery(hql).List<object[]>();
Run Code Online (Sandbox Code Playgroud)
结果列表项是一个数组,其中[0]的类型为Foo,[1] id的类型为Bar
有没有一种方法可以使用Tuple <Foo,Bar>(或其他通用类)作为由object []插入的通用参数,以便可以跳过转换?
是的,您可以这样做,例如:
// The new Tuple<Foo, Bar>(foo, bar) constructor
Type constructor = typeof(Tuple<Foo, Bar>).GetConstructors()[0];
Run Code Online (Sandbox Code Playgroud)
然后
.SetResultTransformer(Transformers.AliasToBeanConstructor(constructor));
Run Code Online (Sandbox Code Playgroud)
之前 .List()
附录
我查看了我的代码(因为一两年前已经完成了此操作:)),我注意到我更喜欢
Type constructor = typeof(Tuple<Foo, Bar>).GetConstructor(new[] { typeof(Foo), typeof(Bar) });
Run Code Online (Sandbox Code Playgroud)
这样做有一个很好的理由:现在(从.NET 4.5.1开始),有一个单一的public构造函数Tuple<T1, T2>,即new Tuple(T1 t1, T2 t2)。有没有隐性或显性担保,在.NET的未来版本将不会有一个无参数的公共构造函数Tuple<T1, T2>将返回一个“空”的元组。较长的GetConstructor格式可确保始终采用“正确的”构造函数。