2011-01-11 74 views
2

是否有可能從NHibernate的查詢返回字典<int, string>像這樣:NHibernate的返回字典<int, string>

CreateQuery("select CustomerId, FullName from Customer") 

我使用.NET ToDictionary方法試圖從這個論壇的幾個例子,但我無法讓他們工作。

回答

5

你需要做的列表或枚舉以下,你應該得到的dictonary

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

我不知道任何方式如何直接在NH做到這一點。 ISession不提供,ICriteria也沒有。 IResultTransformer只是將簡單實體和實體列表轉換爲列表。

當然有解決方法:

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

不過,既然你轉換結果列出只是爲了能夠將其轉換爲字典,這是不理想的。所以有一個額外的轉換降級性能。

相關問題