流利的NHibernate:在字典中返回行
本文关键字:字典 返回 NHibernate | 更新日期: 2023-09-27 18:14:18
我试图从查询中检索字典作为返回值,并且
IQuery query = session.CreateQuery("SELECT Email, COUNT(Id) as IdCount FROM AccountDataModel WHERE Email = :Email")
.SetParameter(":Email", model.Email);
IList<T> list = query.List<T>();
IDictionary<string, object> data = list.ToDictionary<string, object>(x => x); //Error, no method argument matches.
ToDictionary方法要求一个Func类型。我在这里找到了Func的定义。
看到这些后,我尝试了以下操作:
list.ToDictionary<string, object>(y => y.As<string>().As<object>());
我收到一个错误的过程…
是否有一个直接的方法来做到这一点,或者它是一个更hack的方法来促进它?
我认为您需要在代码IList<T> list = query.List<T>();
中替换通用类型参数T
的实际参数类型
比如IList<Email> list = query.List<Email>();
然后,当您调用ToDictionary
时,指定它的键。IDictionary<string, Email> data = list.ToDictionary(x => x.emailId);