NHibernate to return Dictionary <int, string>
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.
精彩评论