NHibernate返回Dictionary <int,string =“”> </int,>

时间:2011-01-11 16:35:29

标签: c# nhibernate dictionary

是否可以从nhibernate查询返回Dictionary <int, string>,如下所示:

CreateQuery("select CustomerId, FullName from Customer")  

我使用.net ToDictionary方法尝试了一些来自此论坛的示例,但我无法让它们工作。

2 个答案:

答案 0 :(得分:5)

您需要在列表或ienumerable上执行以下操作,并且应该获取字典

.ToDictionary(x => x.CustomerId, x => x.FullName);

答案 1 :(得分:2)

我不知道如何直接在NH中这样做。 ISession未提供ICriteriaIResultTransformer只是将简单实体和实体列表转换为列表。

当然有解决方法:

CreateQuery<Customer>("select CustomerId, FullName from Customer")
.ToList<Customer>() // since now working on real objects
.ToDictionary(x => x.CustomerId, x => x.FullName)

但是,这并不理想,因为您将结果转换为list只是为了能够将其转换为字典。因此,还有一个额外的转换会降低性能。