开发者

NHibernate to return Dictionary <int, string>

开发者 https://www.devze.com 2023-02-04 03:57 出处:网络
Is it possible to return Dictionary<int, string> from an nhibernate query such as this: CreateQuery(\"select Cu开发者_Go百科stomerId, FullName from Customer\")

Is it possible to return Dictionary<int, string> from an nhibernate query such as this:

CreateQuery("select Cu开发者_Go百科stomerId, 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.


You need to do the following on your list or enumerable and you should get the dictonary

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


I am not aware of any way how to do this directly in NH. ISession does not provide it, ICriteria neither. IResultTransformer just converts simple entities and lists of entities to lists.

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消