【问题标题】:Fluent NHibernate: returning rows in DictionariesFluent NHibernate:返回字典中的行
【发布时间】:2011-12-25 21:43:06
【问题描述】:

我正在尝试从查询中检索字典作为返回值,并且 我在以下方面运气不佳:

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 的定义,here

看到这个后,我尝试了以下方法:

list.ToDictionary<string, object>(y => y.As<string>().As<object>());

我在处理过程中收到错误...

有没有一种直接的方法可以做到这一点,或者它更像是一种推广它的 hacky 方法?

【问题讨论】:

    标签: c# mysql asp.net-mvc nhibernate fluent-nhibernate


    【解决方案1】:

    我认为您需要在代码IList&lt;T&gt; list = query.List&lt;T&gt;(); 中用实际参数类型代替通用类型参数T

    类似IList&lt;Email&gt; list = query.List&lt;Email&gt;();

    然后,当您调用ToDictionary 时,请指定它的密钥。即.. IDictionary&lt;string, Email&gt; data = list.ToDictionary(x =&gt; x.emailId);

    【讨论】:

    • 电子邮件不是一个对象;这是一个字符串。
    • 您需要有一个映射到查询结果的 Nhibernate 会话对象。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-16
    • 1970-01-01
    • 1970-01-01
    • 2014-09-02
    相关资源
    最近更新 更多