【问题标题】:NHibernate CreateSQLQuery -> Dictionary<int, int>NHibernate CreateSQLQuery -> 字典<int, int>
【发布时间】:2012-01-21 03:07:18
【问题描述】:

我倾向于使用这样的东西:

return NHibernateSession.Current.CreateSQLQuery
(
@"
    some sql
"
)
.SetResultTransformer(NHibernate.Transform.Transformers.AliasToBean(typeof(someviewmodel)))
.List<someviewmodel>();

将我的 sql 输出映射到视图模型。在使用产生两个 int 列的 CreateSQLQuery 时实现与 Dictionary 的相同映射是否非常简单?

谢谢。

【问题讨论】:

    标签: nhibernate


    【解决方案1】:

    您基本上需要创建自己的转换器并在您的SetResultTransformer 调用中指定它。

    它可能看起来像这样:

    public class CustomDictionaryTransformer : IResultTransformer
    {
        public object TransformTuple(object[] tuple, string[] aliases)
        {
            KeyValuePair<int, int> result = new KeyValuePair<int, int>();
            for (int i = 0; i < tuple.Length; i++)
            {
                string alias = aliases[i];
                                var val = new KeyValuePair<int, int>();
                                if (alias == "key") result.Key = (int)tuple[i];
                                else result.Value = (int)tuple[i];
            }
    
            return result;
        }
    
        public IList TransformList(IList collection)
        {
            return collection;
        }
    }
    

    【讨论】:

    • 谢谢。看起来比用 2 个整数编写视图模型更复杂,我暂时已经这样做了。还是谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-11
    • 2010-10-07
    • 2017-07-25
    • 1970-01-01
    相关资源
    最近更新 更多