2011-01-11 52 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) 

不过,既然你转换结果列出只是为了能够将其转换为字典,这是不理想的。所以有一个额外的转换降级性能。

相关问题