none
AutoMapper : from Dictionary<int, string> to List<BlogList> RRS feed

  • Question

  • I have method, which return Dictionary<int, string>, where key is BlogID, value is 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;
            }
        }

    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);
            }

    But in debug i have NullReferenceException in Mapper.Initialize.

    How i can fix this error ?

    Saturday, April 14, 2018 11:18 AM

All replies

  • But in debug i have NullReferenceException in Mapper.Initialize.

    How i can fix this error ?

    Do you know what the error message means?

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

    Saturday, April 14, 2018 8:20 PM
  • Hello dvlprJacob,

    The problem you have encountered is you can't build relationship between dictionary and list, because you can't find property to map. The solutions is you have to think what it is a store of - in this case KeyValuePairs and Bloglist. The code should like below.

                Mapper.Initialize(c =>
                {
                    c.CreateMap<KeyValuePair<int,string>, BlogList>()
                        .ForMember(dest=>dest.BlogID, opt => opt.MapFrom(src => src.Key))
                        .ForMember(dest=>dest.Title, opt => opt.MapFrom(src => src.Value));
                });

    Then invoked by

    List<BlogList> model = Mapper.Map<List<BlogList>>(queryResult);

    There is more detailed info about this.

    AutoMapper Dictionary Flattening

    Best Regards,

    Neil Hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, April 16, 2018 3:20 AM
    Moderator