AutoMapper:from Dictionary< int,string>列出< BlogList> [英] AutoMapper : from Dictionary<int, string> to List<BlogList>

查看:69
本文介绍了AutoMapper:from Dictionary< int,string>列出< BlogList>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有方法,它返回Dictionary< int,string>,其中key是BlogID,value是Title,

I have method, which return Dictionary<int, string>, where key is BlogID, value is Title,

并查看模型BlogList,其属性为BlogID和Title:

and view model BlogList with properties BlogID and Title :

public Dictionary<int, string> GetAllBlogs()
        {
            try
            {
                Dictionary<int, string> result = new Dictionary<int, string>();

                using (var currConnection = this.providerFactory.CreateConnection())
                {

                    currConnection.ConnectionString = this.connectionString;
                    currConnection.Open();

                    SqlCommand selBlogsComm = (SqlCommand)currConnection.CreateCommand();
                    selBlogsComm.CommandText = "SELECT BlogID, Title FROM [dbo].[Blogs] WHERE IsDeleted = 0";

                    using (IDataReader reader = selBlogsComm.ExecuteReader())
                    {
                        while(reader.Read())
                        {
                            result.Add((int)reader[BlogsFields.BlogID], reader[BlogsFields.Title] as string);
                        }
                    }

                    return result;
                }
            }
            catch (Exception exc)
            {
                this.UnresolvedExceptions.Add(exc);
                return null;
            }
        }




public class BlogList
    {
        public int BlogID
        {
            get;
            set;
        }

        public string Title
        {
            get;
            set;
        }
    }

mapper配置init:

And mapper configure init :

public static void ConfigureMaps()
        {
            Mapper.Initialize(c =>
            {
                c.CreateMap<Dictionary<int, string>, List<BlogList>>()
                    .ForMember("BlogID", dest => dest.MapFrom(s => s.Keys))
                    .ForMember("Title", dest => dest.MapFrom(s => s.Values));
                c.CreateMap<Dictionary<int, string>, List<ArticleList>>()
                    .ForMember("ArticleID", dest => dest.MapFrom(s => s.Keys))
                    .ForMember("Title", dest => dest.MapFrom(s => s.Values));
            });

            //Mapper.AssertConfigurationIsValid();
        }

映射执行: 

Mapping exec : 

public ActionResult Index()
        {
            Dictionary<int, string> queryResult = this.repos.GetAllBlogs();
            List<BlogList> model = Mapper.Map<List<BlogList>>(queryResult);

            return View("Index", model);
        }

但是在调试中我在Mapper.Initialize中有NullReferenceException。

But in debug i have NullReferenceException in Mapper.Initialize.

如何我可以解决这个错误吗?

How i can fix this error ?

推荐答案

但是debug我在Mapper.Initialize中有NullReferenceException。

我如何修复这个错误?

你知道错误信息是什么意思吗?

Do you know what the error message means?

如果您知道,那么也许您可以解决错误。

If you knew that, then maybe you can fix the error.


这篇关于AutoMapper:from Dictionary&lt; int,string&gt;列出&lt; BlogList&gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆