简体   繁体   English

NHibernate返回字典<int, string>

[英]NHibernate to return Dictionary <int, string>

Is it possible to return Dictionary <int, string> from an nhibernate query such as this: 是否可以从这样的nhibernate查询返回Dictionary <int, string>

CreateQuery("select CustomerId, FullName from Customer")  

I've tried a few examples from this forum using the .net ToDictionary method, but am unable to get them to work. 我已经使用.net ToDictionary方法尝试了该论坛的一些示例,但无法使其正常工作。

您需要在列表中进行以下操作或枚举,并且应该得到字典

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

I am not aware of any way how to do this directly in NH. 我不知道如何在NH中直接执行此操作。 ISession does not provide it, ICriteria neither. ISession不提供, ICriteria也不提供。 IResultTransformer just converts simple entities and lists of entities to lists. IResultTransformer只是将简单的实体和实体列表转换为列表。

Of course there is workaround: 当然有解决方法:

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

However, this is not ideal since you convert results to list just to be able to convert it to dictionary. 但是,这不是理想的,因为将结果转换为列表只是为了能够将其转换为字典。 So there is one additional transformation degrading performance. 因此,还有一个额外的转换性能下降。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM